<?php

declare(strict_types=1);
/**
 * 用户管理
 * @since   2018-02-06
 * @author  zhaoxiang <zhaoxiang051405@gmail.com>
 */

namespace app\controller\admin;

use app\model\AdminAuthGroupAccess;
use app\model\AdminBudget;
use app\model\AdminBudgetReport;
//use app\model\AdminUserData;
use app\model\AdminProject;
use app\util\ReturnCode;
use app\util\Tools;
use think\facade\Db;
use think\Response;
use PHPExcel_IOFactory;
use PHPExcel;
use PhpOffice\PhpWord\Shared\ZipArchive;

class Budget extends Base
{

    /**
     * 获取用户列表
     * @return Response
     * @throws \think\db\exception\DbException
     * @author zhaoxiang <zhaoxiang051405@gmail.com>
     */
    public function index(): Response
    {
        $limit = $this->request->get('size', config('apiadmin.ADMIN_LIST_DEFAULT'));
        $start = $this->request->get('page', 1);
        $type = $this->request->get('type', '', 'intval');
        $keywords = $this->request->get('keywords', '');
        //        $status = $this->request->get('status', '');

        $obj = new AdminBudget();
        //        if (strlen($status)) {
        //            $obj = $obj->where('status', $status);
        //        }
        //        dump(!Tools::isAdministrator($this->userInfo['id']));
        if (!Tools::isAdministrator($this->userInfo['id'])) {
            // $obj = $obj->where('institutions_code', "{$keywords}");
            $dis_code = substr($this->userInfo['username'], 0, 6);
            $ins_code = substr($this->userInfo['username'], 6, 6);
            // $obj = $obj->where('institutions_code', "{$this->userInfo['username']}");
            $obj = $obj->where('institutions_code', $ins_code);
            $obj = $obj->where('district_code', $dis_code);
        }
        if ($keywords) {
            $obj = $obj->where('institutions_code', "{$keywords}");
        }
        $obj = $obj->where('type', "{$type}");

        $listObj = $obj->order('id', 'ASC')
            ->paginate(['page' => $start, 'list_rows' => $limit])->each(function ($item, $key) {
                $item->userData;
            })->toArray();

        $listInfo = $listObj['data'];

        return $this->buildSuccess([
            'list'  => $listInfo,
            'count' => $listObj['total']
        ]);
    }

    /**
     * 获取用户列表
     * @return Response
     * @throws \think\db\exception\DbException
     * @author zhaoxiang <zhaoxiang051405@gmail.com>
     */
    public function edit(): Response
    {
        $postData = $this->request->post();

        $res = AdminBudget::update($postData);

        if ($res === false) {
            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR);
        }

        return $this->buildSuccess();
    }
    /**
     * 获取用户列表
     * @return Response
     * @throws \think\db\exception\DbException
     * @author zhaoxiang <zhaoxiang051405@gmail.com>
     */
    public function report(): Response
    {
        $limit = $this->request->get('limit', config('apiadmin.ADMIN_LIST_DEFAULT'));
        $start = $this->request->get('start', 1);
        $status = $this->request->get('status', '');
        $depart = $this->request->get('depart', '');
        $keywords = $this->request->get('keywords', '');
        $code = $this->request->get('code', '');
        //        $status = $this->request->get('status', '');

        $obj = new AdminBudgetReport();
        //        if (strlen($status)) {
        //            $obj = $obj->where('status', $status);
        //        }
        //        dump(!Tools::isAdministrator($this->userInfo['id']));
        if (!Tools::isAdministrator($this->userInfo['id'])) {
            // $obj = $obj->where('institutions_code', "{$this->userInfo['username']}");
            $dis_code = substr($this->userInfo['username'], 0, 6);
            $ins_code = substr($this->userInfo['username'], 6, 6);
            // $obj = $obj->where('institutions_code', "{$this->userInfo['username']}");
            // $obj = $obj->where('institutions_code', $ins_code);
            // $obj = $obj->where('district_code', $dis_code);
            $obj = $obj->where('uniq_code', $this->userInfo['username']);
        }
        if ($keywords) {
            // $obj = $obj->where('institutions_code', "{$keywords}");
            $obj = $obj->where([
                ['institutions_name', 'like', "%{$keywords}%"]
            ]);
        }
        if ($status != '') {
            $obj = $obj->where("status", $status);
        }

        if ($depart != '') {
            $obj = $obj->where("departments_code", $depart);
        }
        if ($code != '') {
            $obj = $obj->where("uniq_code", 'like', "%{$code}%");
        }

        $listObj = $obj->order('id', 'ASC')
            ->paginate(['page' => $start, 'list_rows' => $limit])->each(function ($item, $key) {
                $item->userData;
            })->toArray();

        $listInfo = $listObj['data'];

        return $this->buildSuccess([
            'list'  => $listInfo,
            'count' => $listObj['total']
        ]);
    }

    /**
     * 获取用户列表
     * @return Response
     * @throws \think\db\exception\DbException
     * @author zhaoxiang <zhaoxiang051405@gmail.com>
     */
    public function getReportData(): Response
    {
        $code = $this->request->get('code', '');

        $listInfo = $this->getReportDetailData($code);

        return $this->buildSuccess($listInfo);
    }

    /**
     * 获取用户列表
     * @return Response
     * @throws \think\db\exception\DbException
     * @author zhaoxiang <zhaoxiang051405@gmail.com>
     */
    public function saveReport(): Response
    {
        $postData = $this->request->post();
        //        $report = $postData['report'];
        //        if($postData['is_submit']==1){
        //            $report['status'] = 2;
        //        }
        //        $report['office_data'] = json_encode($postData['report']['office_data']);
        //        $report['staffing_data'] = json_encode($postData['report']['staffing_data']);
        //        $report['subordinate_unit_data'] = json_encode($postData['report']['subordinate_unit_data']);
        //        $res = AdminBudgetReport::update($report);
        //        var_dump($postData);
        Db::startTrans();
        try {
            $obj = new AdminBudgetReport();
            if (isset($postData['is_cancel']) && $postData['is_cancel'] == 1) {
                $report['id'] = $postData['id'];
                $report['status'] = 1;
                $obj->update($report);
            } else {
                $report = $postData['report'];
                if (isset($postData['is_submit']) && $postData['is_submit'] == 1) {
                    $report['status'] = 2;
                }
                $report['office_data'] = json_encode($postData['report']['office_data']);
                $report['staffing_data'] = json_encode($postData['report']['staffing_data']);
                $report['subordinate_unit_data'] = json_encode($postData['report']['subordinate_unit_data']);
                $obj->update($report);
            }
            if (isset($postData['project'])) {
                $projectObj = new AdminProject();
                $projectObj->saveAll($postData['project']);
            }
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR);
        }
        //        if ($res === false) {
        //            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR);
        //        }
        return $this->buildSuccess();
    }
    public function import(): Response
    {
        $file_name = root_path() . 'public/uploads/1111.xlsx';   //上传文件的地址
        $name = strrchr('1111.xlsx', '.');
        if ($name == '.xlsx') {
            $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
        } elseif ($name == '.xls') {
            $objReader = \PHPExcel_IOFactory::createReader('Excel5');
        } elseif ($name == '.csv') {
            $objReader = \PHPExcel_IOFactory::createReader('csv');
        }
        $obj_PHPExcel = $objReader->load($file_name, $encode = 'utf-8');  //加载文件内容,编码utf-8

        $excel_array = $obj_PHPExcel->getsheet(0)->toArray();   //转换为数组格式
        // array_shift($excel_array);  //删除第一个数组(标题);
        foreach ($excel_array as $k => $v) {
            $insert = [
                'district_code' => $v[1],
                'district_name' => $v[2],
                'institutions_code' => $v[3],
                'institutions_name' => $v[4],
                'departments_code' => $v[5],
                'departments_name' => $v[6],
                'project_category_code' => $v[7],
                'project_category_name' => $v[8],
                'project_code' => $v[9],
                'project_name' => $v[10],
                'functional_category_code' => $v[11],
                'functional_category_name' => $v[12],
                'functional_category_code_class' => $v[13],
                'functional_category_name_class' => $v[14],
                'functional_category_code_fund' => $v[15],
                'functional_category_name_fund' => $v[16],
                'functional_category_code_item' => $v[17],
                'functional_category_name_item' => $v[18],
                'economics_category_code' => $v[19],
                'economics_category_name' => $v[20],
                'economics_category_code_class' => $v[21],
                'economics_category_name_class' => $v[22],
                'economics_category_code_fund' => $v[23],
                'economics_category_name_fund' => $v[24],
                'total' => $v[25],
                'fiscal_appropriation' => $v[26],
                'general_public_budget' => $v[27],
                'government_budgets' => $v[28],
                'government_capital_management_budget' => $v[29],
                'special_financial_account' => $v[30],
                'utility_income' => $v[31],
                'institution_management_income' => $v[32],
                'institution_other_income' => $v[33],
                'type' => $v[34],
            ];
            $insertData[] = $insert;
        }
        (new AdminBudget())->insertAll($insertData);

        return $this->buildSuccess();
    }
    public function getReportDetailData($code)
    {
        $dis_code = substr($code, 0, 6);
        $ins_code = substr($code, 6, 6);

        $obj = new AdminBudgetReport();
        $listObj = $obj->where('uniq_code', "{$code}")->find()->toArray();

        //table1单位收支总体情况
        //收入
        $countObj = new AdminBudget();
        $countObj = $countObj->where('type', "1");
        $countData = $countObj
            ->field('sum(fiscal_appropriation) fiscal_appropriation_count,sum(general_public_budget) general_public_budget_count,sum(government_budgets) government_budgets_count,sum(government_capital_management_budget) government_capital_management_budget_count,sum(special_financial_account) special_financial_account_count,sum(utility_income) utility_income_count,sum(institution_management_income) institution_management_income_count,sum(institution_other_income) institution_other_income_count')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])->find()->toArray();
        $countData['income_total'] = $countData['fiscal_appropriation_count'] + $countData['special_financial_account_count'] + $countData['utility_income_count'] + $countData['institution_management_income_count'] + $countData['institution_other_income_count']; //收入合计
        //支出
        $budgetObj = new AdminBudget();
        $budgetObj = $budgetObj->where('type', "1");
        $budgetCountData = $budgetObj
            ->field('functional_category_code_class,sum(total) total,sum(general_public_budget) general_public_budget_count,sum(government_budgets) government_budgets_count,sum(government_capital_management_budget) government_capital_management_budget_count')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->group('functional_category_code_class')
            ->select()->toArray();
        $budgetData = array_combine(array_column($budgetCountData, 'functional_category_code_class'), array_column($budgetCountData, 'total'));
        $budgetData['budget_total'] = array_sum($budgetData); //支出合计

        //table2单位收入总体情况表
        $countClassObj = new AdminBudget();
        $countClassObj = $countClassObj->where('type', "1");
        $countClassData = $countClassObj
            ->field('functional_category_code_class,functional_category_name_class name,sum(general_public_budget) general_public_budget_count,sum(government_budgets) government_budgets_count,sum(government_capital_management_budget) government_capital_management_budget_count,sum(special_financial_account) special_financial_account_count,sum(utility_income) utility_income_count,sum(institution_management_income) institution_management_income_count,sum(institution_other_income) institution_other_income_count,economics_category_name_class,project_category_code,project_category_name,sum(total) total')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->group('functional_category_code_class')
            ->orderRaw('functional_category_code_class*1 ASC')->select()->toArray();
        $countFundObj = new AdminBudget();
        $countFundObj = $countFundObj->where('type', "1");
        $countFundData = $countFundObj
            ->field('functional_category_code_class,functional_category_code_fund,functional_category_name_fund name,sum(general_public_budget) general_public_budget_count,sum(government_budgets) government_budgets_count,sum(government_capital_management_budget) government_capital_management_budget_count,sum(special_financial_account) special_financial_account_count,sum(utility_income) utility_income_count,sum(institution_management_income) institution_management_income_count,sum(institution_other_income) institution_other_income_count,economics_category_name_class,project_category_code,project_category_name,sum(total) total')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->group('functional_category_code_class,functional_category_code_fund')
            ->orderRaw('functional_category_code_class*1 ASC,functional_category_code_fund*1 ASC')->select()->toArray();

        $countItemObj = new AdminBudget();
        $countItemObj = $countItemObj->where('type', "1");
        $countItemData = $countItemObj
            ->field('functional_category_code_class,functional_category_code_fund,functional_category_code_item,functional_category_name_item name,sum(general_public_budget) general_public_budget_count,sum(government_budgets) government_budgets_count,sum(government_capital_management_budget) government_capital_management_budget_count,sum(special_financial_account) special_financial_account_count,sum(utility_income) utility_income_count,sum(institution_management_income) institution_management_income_count,sum(institution_other_income) institution_other_income_count,economics_category_name_class,project_name,project_category_code,project_category_name,sum(total) total')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->group('functional_category_code_class,functional_category_code_fund,functional_category_code_item')
            ->orderRaw('functional_category_code_class*1 ASC,functional_category_code_fund*1 ASC,functional_category_code_item*1 ASC')->select()->toArray();

        $countDetailData = [];
        $general_public_budget_count_total = 0;
        $government_budgets_count_total = 0;
        $government_capital_management_budget_count_total = 0;
        $special_financial_account_count_total = 0;
        $utility_income_count_total = 0;
        $institution_management_income_count_total = 0;
        $institution_other_income_count_total = 0;
        $base_budget_total = 0;
        $project_budget_total = 0;
        $income_total = 0;
        $budget_total = 0;
        $general_public_base_budget_total = 0;
        $general_public_project_budget_total = 0;
        foreach ($countClassData as $v) {
            $v['income_total'] = $v['general_public_budget_count'] + $v['government_budgets_count'] + $v['government_capital_management_budget_count'] + $v['special_financial_account_count'] + $v['utility_income_count'] + $v['institution_management_income_count'] + $v['institution_other_income_count'];
            $countDetailData[] = $v;
            $income_total += $v['income_total'];
            $budget_total += $v['total'];
            $general_public_budget_count_total += $v['general_public_budget_count'];
            $government_budgets_count_total += $v['government_budgets_count'];
            $government_capital_management_budget_count_total += $v['government_capital_management_budget_count'];
            $special_financial_account_count_total += $v['special_financial_account_count'];
            $utility_income_count_total += $v['utility_income_count'];
            $institution_management_income_count_total += $v['institution_management_income_count'];
            $institution_other_income_count_total += $v['institution_other_income_count'];
            if ($v['project_category_code'] == 1) {
                $base_budget_total += $v['total'];
                if ($v['general_public_budget_count'] > 0) {
                    $general_public_base_budget_total += $v['total'];
                }
            }
            if ($v['project_category_code'] == 2) {
                $project_budget_total += $v['total'];
                if ($v['general_public_budget_count'] > 0) {
                    $general_public_project_budget_total += $v['total'];
                }
            }
            foreach ($countFundData as $j) {
                if ($j['functional_category_code_class'] == $v['functional_category_code_class']) {
                    $j['income_total'] = $j['general_public_budget_count'] + $j['government_budgets_count'] + $j['government_capital_management_budget_count'] + $j['special_financial_account_count'] + $j['utility_income_count'] + $j['institution_management_income_count'] + $j['institution_other_income_count'];
                    $countDetailData[] = $j;
                    foreach ($countItemData as $l) {
                        if ($l['functional_category_code_class'] == $v['functional_category_code_class'] && $l['functional_category_code_fund'] == $j['functional_category_code_fund']) {
                            $l['income_total'] = $l['general_public_budget_count'] + $l['government_budgets_count'] + $l['government_capital_management_budget_count'] + $l['special_financial_account_count'] + $l['utility_income_count'] + $l['institution_management_income_count'] + $l['institution_other_income_count'];
                            $countDetailData[] = $l;
                        }
                    }
                }
            }
        }

        //table3
        $table3Data = [];
        $table3total = 0;
        $table3total1 = 0;
        $table3total2 = 0;
        foreach ($countClassData as $v) {
            $project_category_code_array = Db::table('admin_budget')
                ->field('project_category_code,sum(total) total')
                ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
                ->where('functional_category_code_class', $v['functional_category_code_class'])
                ->group('project_category_code')->select()->toArray();
            //            var_dump($project_category_code_array);
            if ($project_category_code_array) {
                foreach ($project_category_code_array as $t => $o) {
                    $project_category_code_array[$t]['total'] = floatval($o['total']) / 10000;
                }
                $project_category_code_array = array_combine(array_column($project_category_code_array, 'project_category_code'), $project_category_code_array);
                $v['project_category_code_obj'] = $project_category_code_array;
            } else {
                $v['project_category_code_obj'] = [];
            }
            $table3total += $v['total'];
            $table3total1 += isset($project_category_code_array['1']) ? $project_category_code_array['1']['total'] : 0;
            $table3total2 += isset($project_category_code_array['2']) ? $project_category_code_array['2']['total'] : 0;
            $table3Data[] = $v;
            foreach ($countFundData as $j) {
                if ($j['functional_category_code_class'] == $v['functional_category_code_class']) {
                    $project_category_code_array = Db::table('admin_budget')
                        ->field('project_category_code,sum(total) total')
                        ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
                        ->where('functional_category_code_class', $j['functional_category_code_class'])
                        ->where('functional_category_code_fund', $j['functional_category_code_fund'])
                        ->group('project_category_code')->select()->toArray();
                    if ($project_category_code_array) {
                        foreach ($project_category_code_array as $t => $o) {
                            $project_category_code_array[$t]['total'] = floatval($o['total']) / 10000;
                        }
                        $project_category_code_array = array_combine(array_column($project_category_code_array, 'project_category_code'), $project_category_code_array);
                        $j['project_category_code_obj'] = $project_category_code_array;
                    } else {
                        $j['project_category_code_obj'] = [];
                    }
                    $table3Data[] = $j;
                    foreach ($countItemData as $l) {
                        if ($l['functional_category_code_class'] == $v['functional_category_code_class'] && $l['functional_category_code_fund'] == $j['functional_category_code_fund']) {
                            $l['project_category_code_obj'] = [$l['project_category_code'] => ['name' => $l['project_category_code'], 'total' => floatval($l['total']) / 10000]];
                            $table3Data[] = $l;
                        }
                    }
                }
            }
        }
        //table4
        $budgetCountData = array_combine(array_column($budgetCountData, 'functional_category_code_class'), $budgetCountData);
        //        $general_public_budget_count_total = 0;
        //        $government_budgets_count_total = 0;
        //        $government_capital_management_budget_count_total = 0;
        foreach ($budgetCountData as $k => $v) {
            $budgetCountData[$k]['total'] = $v['total'] / 10000;
            $budgetCountData[$k]['general_public_budget_count'] = $v['general_public_budget_count'] / 10000;
            $budgetCountData[$k]['government_budgets_count'] = $v['government_budgets_count'] / 10000;
            $budgetCountData[$k]['government_capital_management_budget_count'] = $v['government_capital_management_budget_count'] / 10000;
            //            $general_public_budget_count_total += $v['general_public_budget_count']/10000;
            //            $government_budgets_count_total += $v['government_budgets_count']/10000;
            //            $government_capital_management_budget_count_total += $v['government_capital_management_budget_count']/10000;
        }
        //table5
        $general_public_budget_data = [];
        $general_public_budget_data_total = 0;
        $general_public_budget_data_total1 = 0;
        $general_public_budget_data_total2 = 0;
        $general_public_budgetCountClassObj = new AdminBudget();
        $general_public_budgetCountClassObj = $general_public_budgetCountClassObj->where('type', "1");
        $general_public_budgetCountClassData = $general_public_budgetCountClassObj
            ->field('functional_category_code_class,functional_category_name_class name,sum(general_public_budget) general_public_budget_count,economics_category_name_class,project_category_code,project_category_name,sum(total) total')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->where('general_public_budget', '>', 0)
            ->group('functional_category_code_class')
            ->orderRaw('functional_category_code_class*1 ASC')->select()->toArray();
        $general_public_budgetCountFundObj = new AdminBudget();
        $general_public_budgetCountFundObj = $general_public_budgetCountFundObj->where('type', "1");
        $general_public_budgetCountFundData = $general_public_budgetCountFundObj
            ->field('functional_category_code_class,functional_category_code_fund,functional_category_name_fund name,sum(general_public_budget) general_public_budget_count,economics_category_name_class,project_category_code,project_category_name,sum(total) total')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->where('general_public_budget', '>', 0)
            ->group('functional_category_code_class,functional_category_code_fund')
            ->orderRaw('functional_category_code_class*1 ASC,functional_category_code_fund*1 ASC')->select()->toArray();

        $general_public_budgetCountItemObj = new AdminBudget();
        $general_public_budgetCountItemObj = $general_public_budgetCountItemObj->where('type', "1");
        $general_public_budgetCountItemData = $general_public_budgetCountItemObj
            ->field('functional_category_code_class,functional_category_code_fund,functional_category_code_item,functional_category_name_item name,sum(general_public_budget) general_public_budget_count,economics_category_name_class,project_name,project_category_code,project_category_name,sum(total) total')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->where('general_public_budget', '>', 0)
            ->group('functional_category_code_class,functional_category_code_fund,functional_category_code_item')
            ->orderRaw('functional_category_code_class*1 ASC,functional_category_code_fund*1 ASC,functional_category_code_item*1 ASC')->select()->toArray();
        foreach ($general_public_budgetCountClassData as $v) {
            if ($v['general_public_budget_count'] > 0) {
                $project_category_code_array = Db::table('admin_budget')
                    ->field('project_category_code,sum(total) total')->where('type', "1")
                    ->where('general_public_budget', '>', 0)
                    ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
                    ->where('functional_category_code_class', $v['functional_category_code_class'])
                    ->group('project_category_code')->select()->toArray();
                //            var_dump($project_category_code_array);
                if ($project_category_code_array) {
                    foreach ($project_category_code_array as $t => $o) {
                        $project_category_code_array[$t]['total'] = floatval($o['total']) / 10000;
                    }
                    $project_category_code_array = array_combine(array_column($project_category_code_array, 'project_category_code'), $project_category_code_array);
                    $v['project_category_code_obj'] = $project_category_code_array;
                } else {
                    $v['project_category_code_obj'] = [];
                }
                $general_public_budget_data_total += $v['total'];
                $general_public_budget_data_total1 += isset($project_category_code_array['1']) ? $project_category_code_array['1']['total'] : 0;
                $general_public_budget_data_total2 += isset($project_category_code_array['2']) ? $project_category_code_array['2']['total'] : 0;
                $general_public_budget_data[] = $v;
                foreach ($general_public_budgetCountFundData as $j) {
                    if ($j['general_public_budget_count'] > 0) {
                        if ($j['functional_category_code_class'] == $v['functional_category_code_class']) {
                            $project_category_code_array = Db::table('admin_budget')
                                ->field('project_category_code,sum(total) total')->where('type', "1")
                                ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
                                ->where('general_public_budget', '>', 0)
                                ->where('functional_category_code_class', $j['functional_category_code_class'])
                                ->where('functional_category_code_fund', $j['functional_category_code_fund'])
                                ->group('project_category_code')->select()->toArray();
                            if ($project_category_code_array) {
                                foreach ($project_category_code_array as $t => $o) {
                                    $project_category_code_array[$t]['total'] = floatval($o['total']) / 10000;
                                }
                                $project_category_code_array = array_combine(array_column($project_category_code_array, 'project_category_code'), $project_category_code_array);
                                $j['project_category_code_obj'] = $project_category_code_array;
                            } else {
                                $j['project_category_code_obj'] = [];
                            }
                            $general_public_budget_data[] = $j;
                            foreach ($general_public_budgetCountItemData as $l) {
                                if ($l['functional_category_code_class'] == $v['functional_category_code_class'] && $l['functional_category_code_fund'] == $j['functional_category_code_fund']) {
                                    //                                $l['project_category_code_obj'] = [$l['project_category_code'] => ['name' => $l['project_category_code'], 'total' => floatval($l['total']) / 10000]];
                                    $project_category_code_array = Db::table('admin_budget')
                                        ->field('project_category_code,sum(total) total')->where('type', "1")
                                        ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
                                        ->where('general_public_budget', '>', 0)
                                        ->where('functional_category_code_class', $l['functional_category_code_class'])
                                        ->where('functional_category_code_fund', $l['functional_category_code_fund'])
                                        ->where('functional_category_code_item', $l['functional_category_code_item'])
                                        ->group('project_category_code')->select()->toArray();
                                    if ($project_category_code_array) {
                                        foreach ($project_category_code_array as $t => $o) {
                                            $project_category_code_array[$t]['total'] = floatval($o['total']) / 10000;
                                        }
                                        $project_category_code_array = array_combine(array_column($project_category_code_array, 'project_category_code'), $project_category_code_array);
                                        $l['project_category_code_obj'] = $project_category_code_array;
                                    } else {
                                        $l['project_category_code_obj'] = [];
                                    }
                                    $general_public_budget_data[] = $l;
                                }
                            }
                        }
                    }
                }
            }
        }

        //经济分类数据table6
        $economicsClassObj = new AdminBudget();
        $economicsClassData = $economicsClassObj
            ->field('sum(total) total,economics_category_code_class,economics_category_name_class,economics_category_name_class name')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->where('project_category_code', 1)
            ->where('type', "1")
            ->where('general_public_budget', '>', 0)
            ->group('economics_category_code_class')->orderRaw('economics_category_code_class*1 ASC')->select()->toArray();
        $economicsFundObj = new AdminBudget();
        $economicsFundData = $economicsFundObj
            ->field('sum(total) total,economics_category_code_class,economics_category_name_class,economics_category_code_fund,economics_category_name_fund name')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->where('project_category_code', 1)
            ->where('type', "1")
            ->where('general_public_budget', '>', 0)
            ->group('economics_category_code_class,economics_category_code_fund')->orderRaw('economics_category_code_class*1 ASC,economics_category_code_fund*1 ASC')->select()->toArray();

        $economicsDetailData = [];
        $economicsDetailTotal = 0;
        $economicsPeopleTotal = 0;
        $economicsPublicTotal = 0;
        foreach ($economicsClassData as $v) {
            $economicsDetailData[] = $v;
            $economicsDetailTotal += $v['total'];
            if ($v['economics_category_code_class'] == '301' || $v['economics_category_code_class'] == '303') {
                $economicsPeopleTotal += $v['total'];
            }
            if ($v['economics_category_code_class'] == '302') {
                $economicsPublicTotal += $v['total'];
            }
            foreach ($economicsFundData as $j) {
                if ($j['economics_category_code_class'] == $v['economics_category_code_class']) {
                    $economicsDetailData[] = $j;
                }
            }
        }

        //table7一般公共预算项目支出情况表
        //        $countProjectNameObj = new AdminBudget();
        //        $countProjectNameObj = $countProjectNameObj->where('type', "1");
        //        $countProjectNameObj = $countProjectNameObj->where('project_category_code', "2");
        //        $countProjectClassNameData = $countProjectNameObj
        //            ->field('functional_category_code_class,functional_category_name_class name,economics_category_name_class,project_category_code,project_category_name,economics_category_code,sum(total) total')
        //            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
        //            ->group('functional_category_code_class,economics_category_name_class')
        //            ->orderRaw('functional_category_code_class*1 ASC')->select()->toArray();
        //        $countProjectClassNameData = array_combine(array_column($countProjectClassNameData,'functional_category_code_class'),$countProjectClassNameData);

        $countProjectClassObj = new AdminBudget();
        $countProjectClassObj = $countProjectClassObj->where('type', "1");
        $countProjectClassObj = $countProjectClassObj->where('project_category_code', "2");
        $countProjectClassData = $countProjectClassObj
            ->field('functional_category_code_class,functional_category_name_class name,economics_category_name_class,project_category_code,project_category_name,economics_category_code,sum(total) total')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->where('general_public_budget', '>', 0)
            ->group('functional_category_code_class')
            ->orderRaw('functional_category_code_class*1 ASC')->select()->toArray();

        $countProjectFundObj = new AdminBudget();
        $countProjectFundObj = $countProjectFundObj->where('type', "1");
        $countProjectFundObj = $countProjectFundObj->where('project_category_code', "2");
        $countProjectFundData = $countProjectFundObj
            ->field('functional_category_code_class,functional_category_code_fund,functional_category_name_fund name,economics_category_name_class,project_category_code,project_category_name,economics_category_code,sum(total) total')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->where('general_public_budget', '>', 0)
            ->group('functional_category_code_class,functional_category_code_fund')
            ->orderRaw('functional_category_code_class*1 ASC,functional_category_code_fund*1 ASC')->select()->toArray();

        $countProjectItemObj = new AdminBudget();
        $countProjectItemObj = $countProjectItemObj->where('type', "1");
        $countProjectItemObj = $countProjectItemObj->where('project_category_code', "2");
        $countProjectItemData = $countProjectItemObj
            ->field('functional_category_code_class,functional_category_code_fund,functional_category_code_item,functional_category_name_item name,economics_category_name_class,project_name,project_category_code,project_category_name,economics_category_code,sum(total) total,project_code')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->where('general_public_budget', '>', 0)
            ->group('project_code,functional_category_code_class,functional_category_code_fund,functional_category_code_item')
            ->orderRaw('functional_category_code_class*1 ASC,functional_category_code_fund*1 ASC,functional_category_code_item*1 ASC')->select()->toArray();

        $countProjectData = [];
        $countProjectTotal = 0;
        $countProjectTotal1 = 0;
        $countProjectTotal2 = 0;
        $countProjectTotal3 = 0;
        $countProjectTotal4 = 0;
        $countProjectTotal5 = 0;
        $countProjectTotal6 = 0;
        $countProjectTotal7 = 0;
        $countProjectTotal8 = 0;
        $countProjectTotal9 = 0;
        $countProjectTotal10 = 0;
        foreach ($countProjectClassData as $v) {
            $economics_category_name_class_array = Db::table('admin_budget')
                ->field('economics_category_name_class name,sum(total) total')->where('type', "1")
                ->where('general_public_budget', '>', 0)
                ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
                ->where('project_category_code', "2")
                ->where('functional_category_code_class', $v['functional_category_code_class'])
                ->group('economics_category_name_class')->select()->toArray();
            if ($economics_category_name_class_array) {
                foreach ($economics_category_name_class_array as $t => $o) {
                    $economics_category_name_class_array[$t]['total'] = floatval($o['total']) / 10000;
                }
                $economics_category_name_class_array = array_combine(array_column($economics_category_name_class_array, 'name'), $economics_category_name_class_array);
                $v['economics_category_name_class_obj'] = $economics_category_name_class_array;
            } else {
                $v['economics_category_name_class_obj'] = [];
            }
            $countProjectTotal += $v['total'] / 10000;
            $countProjectTotal1 += isset($economics_category_name_class_array['工资福利支出']) ? $economics_category_name_class_array['工资福利支出']['total'] : 0;
            $countProjectTotal2 += isset($economics_category_name_class_array['商品和服务支出']) ? $economics_category_name_class_array['商品和服务支出']['total'] : 0;
            $countProjectTotal3 += isset($economics_category_name_class_array['对个人和家庭的补助']) ? $economics_category_name_class_array['对个人和家庭的补助']['total'] : 0;
            $countProjectTotal4 += isset($economics_category_name_class_array['债务利息及费用支出']) ? $economics_category_name_class_array['债务利息及费用支出']['total'] : 0;
            $countProjectTotal5 += isset($economics_category_name_class_array['资本性支出（基本建设）']) ? $economics_category_name_class_array['资本性支出（基本建设）']['total'] : 0;
            $countProjectTotal6 += isset($economics_category_name_class_array['资本性支出']) ? $economics_category_name_class_array['资本性支出']['total'] : 0;
            $countProjectTotal7 += isset($economics_category_name_class_array['对企业补助（基本建设）']) ? $economics_category_name_class_array['对企业补助（基本建设）']['total'] : 0;
            $countProjectTotal8 += isset($economics_category_name_class_array['对企业补助']) ? $economics_category_name_class_array['对企业补助']['total'] : 0;
            $countProjectTotal9 += isset($economics_category_name_class_array['对社会保障基金补助']) ? $economics_category_name_class_array['对社会保障基金补助']['total'] : 0;
            $countProjectTotal10 += isset($economics_category_name_class_array['其他支出']) ? $economics_category_name_class_array['其他支出']['total'] : 0;
            $countProjectData[] = $v;
            foreach ($countProjectFundData as $j) {
                if ($j['functional_category_code_class'] == $v['functional_category_code_class']) {
                    $economics_category_name_class_array = Db::table('admin_budget')
                        ->field('economics_category_name_class name,sum(total) total')->where('type', "1")
                        ->where('general_public_budget', '>', 0)
                        ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
                        ->where('project_category_code', "2")
                        ->where('functional_category_code_class', $j['functional_category_code_class'])
                        ->where('functional_category_code_fund', $j['functional_category_code_fund'])
                        ->group('economics_category_name_class')->select()->toArray();
                    if ($economics_category_name_class_array) {
                        foreach ($economics_category_name_class_array as $t => $o) {
                            $economics_category_name_class_array[$t]['total'] = floatval($o['total']) / 10000;
                        }
                        $economics_category_name_class_array = array_combine(array_column($economics_category_name_class_array, 'name'), $economics_category_name_class_array);
                        $j['economics_category_name_class_obj'] = $economics_category_name_class_array;
                    } else {
                        $j['economics_category_name_class_obj'] = [];
                    }

                    $countProjectData[] = $j;
                    foreach ($countProjectItemData as $l) {
                        if ($l['functional_category_code_class'] == $v['functional_category_code_class'] && $l['functional_category_code_fund'] == $j['functional_category_code_fund']) {
                            //                            $l['economics_category_name_class_obj'] = [$l['economics_category_name_class']=>['name'=>$l['economics_category_name_class'],'total'=>floatval($l['total'])/10000]];
                            $economics_category_name_class_array = Db::table('admin_budget')
                                ->field('economics_category_name_class name,sum(total) total')->where('type', "1")
                                ->where('general_public_budget', '>', 0)
                                ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
                                ->where('project_category_code', "2")
                                ->where('project_code', $l['project_code'])
                                ->where('functional_category_code_class', $l['functional_category_code_class'])
                                ->where('functional_category_code_fund', $l['functional_category_code_fund'])
                                ->where('functional_category_code_item', $l['functional_category_code_item'])
                                ->group('economics_category_name_class')->select()->toArray();
                            if ($economics_category_name_class_array) {
                                foreach ($economics_category_name_class_array as $t => $o) {
                                    $economics_category_name_class_array[$t]['total'] = floatval($o['total']) / 10000;
                                }
                                $economics_category_name_class_array = array_combine(array_column($economics_category_name_class_array, 'name'), $economics_category_name_class_array);
                                $l['economics_category_name_class_obj'] = $economics_category_name_class_array;
                            } else {
                                $l['economics_category_name_class_obj'] = [];
                            }
                            $countProjectData[] = $l;
                        }
                    }
                }
            }
        }

        //table8三公支出费用
        $threePublicObj = new AdminBudget();
        $threePublicObj = $threePublicObj->where('type', "1");
        $threePublicObj = $threePublicObj->where('economics_category_code', 'in', "30212,30913,31013,30231,30217");
        $threePublic = $threePublicObj
            ->field('economics_category_code,functional_category_code_class,functional_category_name_class name,economics_category_name_class,project_category_code,project_category_name,sum(total) total')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->group('economics_category_code_fund')
            ->orderRaw('economics_category_code_fund*1 ASC')->select()->toArray();
        $threePublic = array_combine(array_column($threePublic, 'economics_category_code'), array_column($threePublic, 'total'));
        $threePublic['total'] = array_sum($threePublic);
        //        var_dump($threePublicObj->getLastSql());

        //table9政府性基金预算支出情况表
        $countGovernmentClassObj = new AdminBudget();
        $countGovernmentClassObj = $countGovernmentClassObj->where('type', "1");
        $countGovernmentClassData = $countGovernmentClassObj
            ->field('functional_category_code_class,functional_category_name_class,economics_category_name_class,project_category_code,project_category_name,sum(total) total,sum(government_budgets) government_budgets_total,government_budgets,functional_category_name name')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->where('government_budgets', '>', 0)
            ->group('functional_category_code_class')
            ->orderRaw('functional_category_code_class*1 ASC')->select()->toArray();
        $countGovernmentFundObj = new AdminBudget();
        $countGovernmentFundObj = $countGovernmentFundObj->where('type', "1");
        $countGovernmentFundData = $countGovernmentFundObj
            ->field('functional_category_code_class,functional_category_code_fund,functional_category_name_fund,economics_category_name_class,project_category_code,project_category_name,sum(total) total,sum(government_budgets) government_budgets_total,government_budgets,functional_category_name name')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->where('government_budgets', '>', 0)
            ->group('functional_category_code_class,functional_category_code_fund')
            ->orderRaw('functional_category_code_class*1 ASC,functional_category_code_fund*1 ASC')->select()->toArray();

        $countGovernmentItemObj = new AdminBudget();
        $countGovernmentItemObj = $countGovernmentItemObj->where('type', "1");
        $countGovernmentItemData = $countGovernmentItemObj
            ->field('functional_category_code_class,functional_category_code_fund,functional_category_code_item,functional_category_name_item,economics_category_name_class,project_name,project_category_code,project_category_name,sum(total) total,sum(government_budgets) government_budgets_total,government_budgets,functional_category_name name')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->where('government_budgets', '>', 0)
            ->group('functional_category_code_class,functional_category_code_fund,functional_category_code_item')
            ->orderRaw('functional_category_code_class*1 ASC,functional_category_code_fund*1 ASC,functional_category_code_item*1 ASC')->select()->toArray();

        $countGovernmentData = [];
        $countGovernmentTotal = 0;
        $countGovernmentProjectTotal = 0;
        $countGovernmentBaseTotal = 0;
        foreach ($countGovernmentClassData as $v) {
            $countGovernmentData[] = $v;
            $countGovernmentTotal += $v['government_budgets_total'];
            if ($v['project_category_code'] == 1) {
                $countGovernmentBaseTotal += $v['government_budgets_total'];
            }
            if ($v['project_category_code'] == 2) {
                $countGovernmentProjectTotal += $v['government_budgets_total'];
            }
            foreach ($countGovernmentFundData as $j) {
                if ($j['functional_category_code_class'] == $v['functional_category_code_class']) {
                    $countGovernmentData[] = $j;
                    foreach ($countGovernmentItemData as $l) {
                        if ($l['functional_category_code_class'] == $v['functional_category_code_class'] && $l['functional_category_code_fund'] == $j['functional_category_code_fund']) {
                            $countGovernmentData[] = $l;
                        }
                    }
                }
            }
        }

        //机关运行经费
        $officeRunFundingObj = new AdminBudget();
        $officeRunFundingObj = $officeRunFundingObj->where('type', "1");
        $officeRunFunding = $officeRunFundingObj->field('economics_category_name')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->where('economics_category_code_class', '302')
            ->where('fiscal_appropriation', '>', 0)
            ->group('economics_category_code,economics_category_name')->column('economics_category_name');
        array_unique($officeRunFunding);


        $projectObj = new AdminProject();
        // $projectList = $projectObj->where(['institutions_code'=>$ins_code,'district_code'=>$dis_code])->select()->toArray();
        $projectList = $projectObj->where(['uniq_code' => $code])->select()->toArray();

        //处理数据
        foreach ($projectList as $k => $v) {
            $projectList[$k]['budget_size'] = number_format($v['budget_size'] / 10000, 2, ".", "");
        }
        foreach ($countData as $k => $v) {
            $countData[$k] = $v / 10000;
        }
        foreach ($budgetData as $k => $v) {
            $budgetData[$k] = $v / 10000;
        }
        foreach ($countDetailData as $k => $v) {
            $v['total'] = $v['total'] / 10000;
            $v['income_total'] = $v['income_total'] / 10000;
            $v['general_public_budget_count'] = $v['general_public_budget_count'] / 10000;
            $v['government_budgets_count'] = $v['government_budgets_count'] / 10000;
            $v['government_capital_management_budget_count'] = $v['government_capital_management_budget_count'] / 10000;
            $v['special_financial_account_count'] = $v['special_financial_account_count'] / 10000;
            $v['utility_income_count'] = $v['utility_income_count'] / 10000;
            $v['institution_management_income_count'] = $v['institution_management_income_count'] / 10000;
            $v['institution_other_income_count'] = $v['institution_other_income_count'] / 10000;
            $countDetailData[$k] = $v;
        }
        foreach ($countProjectData as $k => $v) {
            $v['total'] = $v['total'] / 10000;
            $countProjectData[$k] = $v;
        }
        foreach ($economicsDetailData as $k => $v) {
            $v['total'] = $v['total'] / 10000;
            $economicsDetailData[$k] = $v;
        }

        foreach ($threePublic as $k => $v) {
            $threePublic[$k] = $v / 10000;
        }
        foreach ($table3Data as $k => $v) {
            $v['total'] = $v['total'] / 10000;
            $table3Data[$k] = $v;
        }
        foreach ($general_public_budget_data as $k => $v) {
            $v['total'] = $v['total'] / 10000;
            $general_public_budget_data[$k] = $v;
        }
        // foreach($countGovernmentData as $k=>$v) {
        //     $countGovernmentData[$k] = $v['total'] / 10000;
        // }

        $listInfo['obj'] = $listObj;
        $listInfo['incomeCount'] = $countData;
        $listInfo['budgetCount'] = $budgetData;
        $listInfo['budgetCountData'] = [
            'list' => $budgetCountData,
            'general_public_budget_count' => $general_public_budget_count_total / 10000,
            'government_budgets_count' => $government_budgets_count_total / 10000,
            'government_capital_management_budget_count' => $government_capital_management_budget_count_total / 10000,
        ];
        $listInfo['countDetailData'] = [
            'list' => $countDetailData,
            'general_public_budget_count_total' => $general_public_budget_count_total / 10000,
            'government_budgets_count_total' => $government_budgets_count_total / 10000,
            'government_capital_management_budget_count_total' => $government_capital_management_budget_count_total / 10000,
            'special_financial_account_count_total' => $special_financial_account_count_total / 10000,
            'utility_income_count_total' => $utility_income_count_total / 10000,
            'institution_management_income_count_total' => $institution_management_income_count_total / 10000,
            'institution_other_income_count_total' => $institution_other_income_count_total / 10000,
            'base_budget_total' => $base_budget_total / 10000,
            'project_budget_total' => $project_budget_total / 10000,
            'income_total' => $income_total / 10000,
            'budget_total' => $budget_total / 10000,
            'general_public_base_budget_total' => $general_public_base_budget_total / 10000,
            'general_public_project_budget_total' => $general_public_project_budget_total / 10000,
        ];
        $listInfo['countClassData'] = $countClassData;
        $listInfo['countFundData'] = $countFundData;
        $listInfo['countItemData'] = $countItemData;
        $listInfo['general_public_budget_data'] = [
            'list' => $general_public_budget_data,
            'general_public_budget_data_total' => $general_public_budget_data_total / 10000,
            'general_public_budget_data_total1' => $general_public_budget_data_total1,
            'general_public_budget_data_total2' => $general_public_budget_data_total2,
        ];
        $listInfo['economicsDetailData'] = [
            'list' => $economicsDetailData,
            'economicsDetailTotal' => $economicsDetailTotal / 10000,
            'economicsPeopleTotal' => $economicsPeopleTotal / 10000,
            'economicsPublicTotal' => $economicsPublicTotal / 10000
        ];
        $listInfo['countProjectData'] = [
            'list' => $countProjectData,
            'countProjectTotal' => $countProjectTotal,
            'countProjectTotal1' => $countProjectTotal1,
            'countProjectTotal2' => $countProjectTotal2,
            'countProjectTotal3' => $countProjectTotal3,
            'countProjectTotal4' => $countProjectTotal4,
            'countProjectTotal5' => $countProjectTotal5,
            'countProjectTotal6' => $countProjectTotal6,
            'countProjectTotal7' => $countProjectTotal7,
            'countProjectTotal8' => $countProjectTotal8,
            'countProjectTotal9' => $countProjectTotal9,
            'countProjectTotal10' => $countProjectTotal10,
        ];
        $listInfo['threePublic'] = $threePublic;
        $listInfo['countGovernmentData'] = [
            'list' => $countGovernmentData,
            'countGovernmentTotal' => $countGovernmentTotal,
            'countGovernmentProjectTotal' => $countGovernmentProjectTotal,
            'countGovernmentBaseTotal' => $countGovernmentBaseTotal,
        ];
        $listInfo['officeRunFunding'] = $officeRunFunding;
        $listInfo['projectList'] = $projectList;
        $listInfo['origin'] = $listObj;
        $listInfo['table3Data'] = [
            'list' => $table3Data,
            'table3Total' => $table3total / 10000,
            'table3Total1' => $table3total1,
            'table3Total2' => $table3total2,
        ];

        return $listInfo;
    }
    public function download()
    {
        $code = $this->request->post('code', '');
        $file = $this->getDownload($code);
        return $file['file'];
    }
    public function batchDownload()
    {
        $code = $this->request->post('codes', '');
        $zip_name = root_path() . 'public/uploads/report.zip';
        $zipObj = new \ZipArchive();
        $zipObj->open($zip_name, \ZipArchive::CREATE);
        foreach ($code as $v) {
            $file =  $this->getDownload($v);
            $name = $file['name'];
            $zipObj->addFile($name, basename($name));
        }
        $zipObj->close();
        return download($zip_name);
    }
    public function getDownload($code)
    {
        $data = $this->getReportDetailData($code);
        $temp_name = root_path() . 'public/uploads/report.docx';   //模板文件的地址
        $tmp = new \PhpOffice\PhpWord\TemplateProcessor($temp_name); //打开模板

        //第一部分
        $tmp->setValue('institutions_name', $data['obj']['institutions_name']); //替换变量name
        $tmp->setValue('main_function', str_replace(PHP_EOL, PHP_EOL . '<w:br/>', $data['obj']['main_function'])); //替换变量main_function
        $tmp->setValue('office_data', implode('、', json_decode($data['obj']['office_data']))); //替换变量office_data
        $tmp->setValue('office_num', count(json_decode($data['obj']['office_data'])));
        if ($data['obj']['has_subordinate_unit']) {
            $tmp->cloneBlock('UNITBLOCK', 1);
            $tmp->cloneBlock('NOUNITBLOCK', 0);
            $tmp->setValue('subordinate_unit_data', implode('、', json_decode($data['obj']['subordinate_unit_data']))); //替换变量office_data
            $tmp->setValue('subordinate_unit_num', $data['obj']['subordinate_unit_num']); //替换变量office_data
            $tmp->setValue('administrative_unit_num', $data['obj']['administrative_unit_num']); //替换变量office_data
            $tmp->setValue('public_unit_num', $data['obj']['public_unit_num']); //替换变量office_data
        } else {
            $tmp->cloneBlock('NOUNITBLOCK', 1);
            $tmp->cloneBlock('UNITBLOCK', 0);
        }
        $tmp->setValue('staffing_num', $data['obj']['staffing_num']);
        $tmp->setValue('actual_num', $data['obj']['actual_num']);
        $staffing_data = json_decode($data['obj']['staffing_data'], true);
        $tmp->setValue('onJobCount', $staffing_data['onJob']['count']);
        $tmp->setValue('onJobChangeType', $staffing_data['onJob']['changeType'] == 1 ? '增加' : '减少');
        $tmp->setValue('onJobChangeNum', $staffing_data['onJob']['changeNum']);
        $tmp->setValue('retirementCount', $staffing_data['retirement']['count']);
        $tmp->setValue('retirementChangeType', $staffing_data['retirement']['changeType'] == 1 ? '增加' : '减少');
        $tmp->setValue('retirementChangeNum', $staffing_data['retirement']['changeNum']);
        $tmp->setValue('retireCount', $staffing_data['retire']['count']);
        $tmp->setValue('retireChangeType', $staffing_data['retire']['changeType'] == 1 ? '增加' : '减少');
        $tmp->setValue('retireChangeNum', $staffing_data['retire']['changeNum']);

        //第二部分
        //table1
        foreach ($data['incomeCount'] as $k => $v) {
            $tmp->setValue($k, $v);
        }
        $budget_code = [201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 219, 220, 221, 222, 223, 224, 227, 229, 230, 231, 232, 233, 234];
        foreach ($budget_code as $v) {
            if (isset($data['budgetCount'][$v])) {
                $tmp->setValue($v, $data['budgetCount'][$v]);
            } else {
                $tmp->setValue($v, '');
            }
        }
        $tmp->setValue('budget_total', $data['budgetCount']['budget_total']);

        //table2
        $rows = count($data['countDetailData']['list']); //总行数
        $tmp->cloneRow('table2_class', $rows); //复制行
        foreach ($data['countDetailData']['list'] as $k => $v) {
            $tmp->setValue('table2_class#' . ($k + 1), isset($v['functional_category_code_class']) ? $v['functional_category_code_class'] : '');
            $tmp->setValue('table2_fund#' . ($k + 1), isset($v['functional_category_code_fund']) ? $v['functional_category_code_fund'] : '');
            $tmp->setValue('table2_item#' . ($k + 1), isset($v['functional_category_code_item']) ? $v['functional_category_code_item'] : '');
            $tmp->setValue('table2_name#' . ($k + 1), $v['name']);
            $tmp->setValue('table2_income_total#' . ($k + 1), $v['income_total']);
            $tmp->setValue('table2_general_public_budget_count#' . ($k + 1), isset($v['general_public_budget_count']) ? $v['general_public_budget_count'] : '');
            $tmp->setValue('table2_government_budgets_count#' . ($k + 1), isset($v['government_budgets_count']) ? $v['government_budgets_count'] : '');
            $tmp->setValue('table2_government_capital_management_budget_count#' . ($k + 1), isset($v['government_capital_management_budget_count']) ? $v['government_capital_management_budget_count'] : '');
            $tmp->setValue('table2_special_financial_account_count#' . ($k + 1), isset($v['special_financial_account_count']) ? $v['special_financial_account_count'] : '');
            $tmp->setValue('table2_utility_income_count#' . ($k + 1), isset($v['utility_income_count']) ? $v['utility_income_count'] : '');
            $tmp->setValue('table2_institution_management_income_count#' . ($k + 1), isset($v['institution_management_income_count']) ? $v['institution_management_income_count'] : '');
            $tmp->setValue('table2_institution_other_income_count#' . ($k + 1), isset($v['institution_other_income_count']) ? $v['institution_other_income_count'] : '');
        }
        $tmp->setValue('general_public_budget_count_total', isset($data['countDetailData']['general_public_budget_count_total']) ? $data['countDetailData']['general_public_budget_count_total'] : '');
        $tmp->setValue('government_budgets_count_total', isset($data['countDetailData']['government_budgets_count_total']) ? $data['countDetailData']['government_budgets_count_total'] : '');
        $tmp->setValue('government_capital_management_budget_count_total', isset($data['countDetailData']['government_capital_management_budget_count_total']) ? $data['countDetailData']['government_capital_management_budget_count_total'] : '');
        $tmp->setValue('special_financial_account_count_total', isset($data['countDetailData']['special_financial_account_count_total']) ? $data['countDetailData']['special_financial_account_count_total'] : '');
        $tmp->setValue('utility_income_count_total', isset($data['countDetailData']['utility_income_count_total']) ? $data['countDetailData']['utility_income_count_total'] : '');
        $tmp->setValue('institution_management_income_count_total', isset($data['countDetailData']['institution_management_income_count_total']) ? $data['countDetailData']['institution_management_income_count_total'] : '');
        $tmp->setValue('institution_other_income_count_total', isset($data['countDetailData']['institution_other_income_count_total']) ? $data['countDetailData']['institution_other_income_count_total'] : '');

        //table3
        $tmp->cloneRow('table3_class', $rows); //复制行
        foreach ($data['countDetailData']['list'] as $k => $v) {
            $tmp->setValue('table3_class#' . ($k + 1), isset($v['functional_category_code_class']) ? $v['functional_category_code_class'] : '');
            $tmp->setValue('table3_fund#' . ($k + 1), isset($v['functional_category_code_fund']) ? $v['functional_category_code_fund'] : '');
            $tmp->setValue('table3_item#' . ($k + 1), isset($v['functional_category_code_item']) ? $v['functional_category_code_item'] : '');
            $tmp->setValue('table3_name#' . ($k + 1), $v['name']);
            $tmp->setValue('table3_total#' . ($k + 1), $v['total']);
            $tmp->setValue('table3_base_total#' . ($k + 1), $v['project_category_code'] == 1 ? $v['total'] : '');
            $tmp->setValue('table3_project_total#' . ($k + 1), $v['project_category_code'] == 2 ? $v['total'] : '');
        }
        $tmp->setValue('budget_total', isset($data['countDetailData']['budget_total']) ? $data['countDetailData']['budget_total'] : '');
        $tmp->setValue('base_budget_total', isset($data['countDetailData']['base_budget_total']) ? $data['countDetailData']['base_budget_total'] : '');
        $tmp->setValue('project_budget_total', isset($data['countDetailData']['project_budget_total']) ? $data['countDetailData']['project_budget_total'] : '');

        //table4
        $countClassData = array_combine(array_column($data['countClassData'], 'functional_category_code_class'), $data['countClassData']);
        foreach ($budget_code as $v) {
            if (isset($countClassData[$v])) {
                $tmp->setValue($v . 'total', $countClassData[$v]['total']);
                $tmp->setValue($v . 'general_public_budget_total', $countClassData[$v]['general_public_budget_count']);
                $tmp->setValue($v . 'government_budgets_total', $countClassData[$v]['government_budgets_count']);
                $tmp->setValue($v . 'government_capital_management_budget_total', $countClassData[$v]['government_capital_management_budget_count']);
            } else {
                $tmp->setValue($v . 'total', '');
                $tmp->setValue($v . 'general_public_budget_total', '');
                $tmp->setValue($v . 'government_budgets_total', '');
                $tmp->setValue($v . 'government_capital_management_budget_total', '');
            }
        }
        //table5
        $tmp->cloneRow('table5_class', $rows); //复制行
        foreach ($data['countDetailData']['list'] as $k => $v) {
            $tmp->setValue('table5_class#' . ($k + 1), isset($v['functional_category_code_class']) ? $v['functional_category_code_class'] : '');
            $tmp->setValue('table5_fund#' . ($k + 1), isset($v['functional_category_code_fund']) ? $v['functional_category_code_fund'] : '');
            $tmp->setValue('table5_item#' . ($k + 1), isset($v['functional_category_code_item']) ? $v['functional_category_code_item'] : '');
            $tmp->setValue('table5_name#' . ($k + 1), $v['name']);
            $tmp->setValue('table5_total#' . ($k + 1), $v['total']);
            $tmp->setValue('table5_base_total#' . ($k + 1), $v['project_category_code'] == 1 ? $v['total'] : '');
            $tmp->setValue('table5_project_total#' . ($k + 1), $v['project_category_code'] == 2 ? $v['total'] : '');
        }

        //table6
        $tmp->cloneRow('table6_class', count($data['economicsDetailData']['list'])); //复制行
        foreach ($data['economicsDetailData']['list'] as $k => $v) {
            $tmp->setValue('table6_class#' . ($k + 1), isset($v['economics_category_code_class']) ? $v['economics_category_code_class'] : '');
            $tmp->setValue('table6_fund#' . ($k + 1), isset($v['economics_category_code_fund']) ? $v['economics_category_code_fund'] : '');
            $tmp->setValue('table6_name#' . ($k + 1), $v['name']);
            $tmp->setValue('table6_total#' . ($k + 1), $v['total']);
            $tmp->setValue('table6_people_total#' . ($k + 1), $v['economics_category_code_class'] == 301 || $v['economics_category_code_class'] == 303 ? $v['total'] : '');
            $tmp->setValue('table6_public_total#' . ($k + 1), $v['economics_category_code_class'] == 302 ? $v['total'] : '');
        }

        $tmp->setValue('economicsDetailTotal', $data['economicsDetailData']['economicsDetailTotal']);
        $tmp->setValue('economicsPeopleTotal', $data['economicsDetailData']['economicsPeopleTotal']);
        $tmp->setValue('economicsPublicTotal', $data['economicsDetailData']['economicsPublicTotal']);

        //table7
        $tmp->cloneRow('table7_class', count($data['countProjectData']['list'])); //复制行
        foreach ($data['countProjectData']['list'] as $k => $v) {
            $tmp->setValue('table7_class#' . ($k + 1), isset($v['functional_category_code_class']) ? $v['functional_category_code_class'] : '');
            $tmp->setValue('table7_fund#' . ($k + 1), isset($v['functional_category_code_fund']) ? $v['functional_category_code_fund'] : '');
            $tmp->setValue('table7_item#' . ($k + 1), isset($v['functional_category_code_item']) ? $v['functional_category_code_item'] : '');
            $tmp->setValue('table7_name#' . ($k + 1), $v['name']);
            $tmp->setValue('table7_project_name#' . ($k + 1), isset($v['project_name']) ? $v['project_name'] : '');
            $tmp->setValue('table7_total#' . ($k + 1), $v['total']);
            $tmp->setValue('table7_total1#' . ($k + 1), isset($v['economics_category_name_class_obj']['工资福利支出']) ? $v['economics_category_name_class_obj']['工资福利支出']['total'] : '');
            $tmp->setValue('table7_total2#' . ($k + 1), isset($v['economics_category_name_class_obj']['商品和服务支出']) ? $v['economics_category_name_class_obj']['商品和服务支出']['total'] : '');
            $tmp->setValue('table7_total3#' . ($k + 1), isset($v['economics_category_name_class_obj']['对个人和家庭的补助']) ? $v['economics_category_name_class_obj']['对个人和家庭的补助']['total'] : '');
            $tmp->setValue('table7_total4#' . ($k + 1), isset($v['economics_category_name_class_obj']['债务利息及费用支出']) ? $v['economics_category_name_class_obj']['债务利息及费用支出']['total'] : '');
            $tmp->setValue('table7_total5#' . ($k + 1), isset($v['economics_category_name_class_obj']['资本性支出（基本建设）']) ? $v['economics_category_name_class_obj']['资本性支出（基本建设）']['total'] : '');
            $tmp->setValue('table7_total6#' . ($k + 1), isset($v['economics_category_name_class_obj']['资本性支出']) ? $v['economics_category_name_class_obj']['资本性支出']['total'] : '');
            $tmp->setValue('table7_total7#' . ($k + 1), isset($v['economics_category_name_class_obj']['对企业补助（基本建设）']) ? $v['economics_category_name_class_obj']['对企业补助（基本建设）']['total'] : '');
            $tmp->setValue('table7_total8#' . ($k + 1), isset($v['economics_category_name_class_obj']['对企业补助']) ? $v['economics_category_name_class_obj']['对企业补助']['total'] : '');
            $tmp->setValue('table7_total9#' . ($k + 1), isset($v['economics_category_name_class_obj']['对社会保障基金补助']) ? $v['economics_category_name_class_obj']['对社会保障基金补助']['total'] : '');
            $tmp->setValue('table7_total10#' . ($k + 1), isset($v['economics_category_name_class_obj']['其他支出']) ? $v['economics_category_name_class_obj']['其他支出']['total'] : '');
        }
        $tmp->setValue('table7_project_total', $data['countProjectData']['countProjectTotal']);
        $tmp->setValue('table7_project_total1', $data['countProjectData']['countProjectTotal1'] ? $data['countProjectData']['countProjectTotal1'] : '');
        $tmp->setValue('table7_project_total2', $data['countProjectData']['countProjectTotal2'] ? $data['countProjectData']['countProjectTotal2'] : '');
        $tmp->setValue('table7_project_total3', $data['countProjectData']['countProjectTotal3'] ? $data['countProjectData']['countProjectTotal3'] : '');
        $tmp->setValue('table7_project_total4', $data['countProjectData']['countProjectTotal4'] ? $data['countProjectData']['countProjectTotal4'] : '');
        $tmp->setValue('table7_project_total5', $data['countProjectData']['countProjectTotal5'] ? $data['countProjectData']['countProjectTotal5'] : '');
        $tmp->setValue('table7_project_total6', $data['countProjectData']['countProjectTotal6'] ? $data['countProjectData']['countProjectTotal6'] : '');
        $tmp->setValue('table7_project_total7', $data['countProjectData']['countProjectTotal7'] ? $data['countProjectData']['countProjectTotal7'] : '');
        $tmp->setValue('table7_project_total8', $data['countProjectData']['countProjectTotal8'] ? $data['countProjectData']['countProjectTotal8'] : '');
        $tmp->setValue('table7_project_total9', $data['countProjectData']['countProjectTotal9'] ? $data['countProjectData']['countProjectTotal9'] : '');
        $tmp->setValue('table7_project_total10', $data['countProjectData']['countProjectTotal10'] ? $data['countProjectData']['countProjectTotal10'] : '');

        //table8
        $tmp->setValue('threePublic_total', $data['threePublic']['total']);
        $tmp->setValue('threePublic_trip_total', isset($data['threePublic']['30212']) ? floatval($data['threePublic']['30212']) : '');
        $tmp->setValue('threePublic_car_total', isset($data['threePublic']['30913']) ? floatval($data['threePublic']['30913']) : 0 + isset($data['threePublic']['31013']) ? floatval($data['threePublic']['31013']) : 0 + isset($data['threePublic']['30231']) ? floatval($data['threePublic']['30231']) : 0);
        $tmp->setValue('threePublic_buy_car_total', isset($data['threePublic']['30913']) ? floatval($data['threePublic']['30913']) : 0 + isset($data['threePublic']['31013']) ? floatval($data['threePublic']['31013']) : 0);
        $tmp->setValue('threePublic_use_car_total', isset($data['threePublic']['30231']) ? floatval($data['threePublic']['30231']) : '');
        $tmp->setValue('threePublic_jiedai_total', isset($data['threePublic']['30217']) ? floatval($data['threePublic']['30217']) : '');


        //table9
        $tmp->cloneRow('table9_class', count($data['countGovernmentData']['list'])); //复制行
        foreach ($data['countGovernmentData']['list'] as $k => $v) {
            $tmp->setValue('table9_class#' . ($k + 1), isset($v['functional_category_code_class']) ? $v['functional_category_code_class'] : '');
            $tmp->setValue('table9_fund#' . ($k + 1), isset($v['functional_category_code_fund']) ? $v['functional_category_code_fund'] : '');
            $tmp->setValue('table9_item#' . ($k + 1), isset($v['functional_category_code_item']) ? $v['functional_category_code_item'] : '');
            $tmp->setValue('table9_name#' . ($k + 1), $v['name']);
            $tmp->setValue('table9_total#' . ($k + 1), $v['total']);
            $tmp->setValue('table9_base_total#' . ($k + 1), $v['project_category_code'] == 1 ? $v['total'] : '');
            $tmp->setValue('table9_project_total#' . ($k + 1), $v['project_category_code'] == 2 ? $v['total'] : '');
        }
        $tmp->setValue('table9_budget_total', $data['countGovernmentData']['countGovernmentTotal'] ? $data['countGovernmentData']['countGovernmentTotal'] : '');
        $tmp->setValue('table9_base_budget_total', $data['countGovernmentData']['countGovernmentBaseTotal'] ? $data['countGovernmentData']['countGovernmentBaseTotal'] : '');
        $tmp->setValue('table9_project_budget_total', $data['countGovernmentData']['countGovernmentProjectTotal'] ? $data['countGovernmentData']['countGovernmentProjectTotal'] : '');

        //第三部分
        $part_three_data = $this->getPartThreeData($code);
        //        var_dump($part_three_data);
        $tmp->setValue('year1', $part_three_data['year']);
        $tmp->setValue('month1', $part_three_data['month']);
        $tmp->setValue('day1', $part_three_data['day']);
        //3-1
        $tmp->setValue('zz1', round($part_three_data['thisyear']['total'], 2));
        $tmp->setValue('zz2', round($part_three_data['thisyear']['general_public_budget'], 2));
        $tmp->setValue('zz3', round($part_three_data['thisyear']['government_budgets'], 2));
        $tmp->setValue('zz4', round($part_three_data['thisyear']['institution_other_income'], 2));
        $tmp->setValue('shbzjyzc', round($part_three_data['thisyear']['shbzjyzc'], 2));
        $tmp->setValue('wsjkzc', round($part_three_data['thisyear']['wsjkzc'], 2));
        $tmp->setValue('jnhbzc', round($part_three_data['thisyear']['jnhbzc'], 2));
        $tmp->setValue('cxsqzc', round($part_three_data['thisyear']['cxsqzc'], 2));
        $tmp->setValue('nlszc', round($part_three_data['thisyear']['nlszc'], 2));
        $tmp->setValue('zrzyhyqxzc', round($part_three_data['thisyear']['zrzyhyqxzc'], 2));
        $tmp->setValue('zfbzzc', round($part_three_data['thisyear']['zfbzzc'], 2));
        $tmp->setValue('zhfzzc', round($part_three_data['thisyear']['zhfzzc'], 2));
        // 3-2
        // 一般公共预算
        $tmp->setValue('z1', round($part_three_data['thisyear']['general_public_budget'], 2));
        $tmp->setValue('z2', str_replace("-", "", round($part_three_data['calc']['v1'], 2)));
        $tmp->setValue('z3', str_replace("-", "", round($part_three_data['calc']['v2'], 2)));
        $tmp->setValue('m3', $this->judge($part_three_data['calc']['v2']));
        $tmp->setValue('z4', str_replace("-", "", round($part_three_data['calc']['v3'], 2)));
        // 政府性基金
        $tmp->setValue('z5', round($part_three_data['thisyear']['government_budgets'], 2));
        $tmp->setValue('z6', str_replace("-", "", round($part_three_data['calc']['v4'], 2)));
        $tmp->setValue('z7', str_replace("-", "", round($part_three_data['calc']['v5'], 2)));
        $tmp->setValue('m7', $this->judge($part_three_data['calc']['v5']));
        $tmp->setValue('z8', str_replace("-", "", round($part_three_data['calc']['v6'], 2)));
        // 其他资金
        $tmp->setValue('z9', round($part_three_data['thisyear']['institution_other_income'], 2));
        $tmp->setValue('z10', str_replace("-", "", round($part_three_data['calc']['v7'], 2)));
        $tmp->setValue('z11', str_replace("-", "", round($part_three_data['calc']['v8'], 2)));
        $tmp->setValue('m11', $this->judge($part_three_data['calc']['v8']));
        $tmp->setValue('z12', str_replace("-", "", round($part_three_data['calc']['v9'], 2)));
        // 3-3
        // total
        $tmp->setValue('ztotal', round($part_three_data['thisyear']['total'], 2));
        // 基本支出
        $tmp->setValue('z13', round($part_three_data['thisyear']['jbzc'], 2));
        $tmp->setValue('z14', str_replace("-", "", round($part_three_data['calc']['v10'], 2)));
        $tmp->setValue('z15', str_replace("-", "", round($part_three_data['calc']['v11'], 2)));
        $tmp->setValue('m15', $this->judge($part_three_data['calc']['v11']));
        $tmp->setValue('z16', str_replace("-", "", round($part_three_data['calc']['v12'], 2)));
        // 项目支出
        $tmp->setValue('z17', round($part_three_data['thisyear']['xmzc'], 2));
        $tmp->setValue('z18', str_replace("-", "", round($part_three_data['calc']['v13'], 2)));
        $tmp->setValue('z19', str_replace("-", "", round($part_three_data['calc']['v14'], 2)));
        $tmp->setValue('m19', $this->judge($part_three_data['calc']['v14']));
        $tmp->setValue('z20', str_replace("-", "", round($part_three_data['calc']['v15'], 2)));
        // 3-4
        // 财政拨款
        $tmp->setValue('z21', round($part_three_data['thisyear']['fiscal_appropriation'], 2));
        // 收入预算
        $tmp->setValue('z22', round($part_three_data['thisyear']['general_public_budget'], 2));
        $tmp->setValue('z23', round($part_three_data['thisyear']['government_budgets'], 2));
        // 一般公共预算支出
        $tmp->setValue('z24', round($part_three_data['thisyear']['shbzjyzc'], 2));
        $tmp->setValue('z25', round($part_three_data['thisyear']['wsjkzc'], 2));
        $tmp->setValue('z26', round($part_three_data['thisyear']['jnhbzc'], 2));
        $tmp->setValue('z27', round($part_three_data['thisyear']['nlszc'], 2));
        $tmp->setValue('z28', round($part_three_data['thisyear']['zrzyhyqxzc'], 2));
        $tmp->setValue('z29', round($part_three_data['thisyear']['zfbzzc'], 2));
        $tmp->setValue('z30', round($part_three_data['thisyear']['zhfzzc'], 2));
        // 政府性基金预算拨款
        // $tmp->setValue('z31', round($part_three_data['thisyear']['government_budgets'], 2));
        // 一般公共预算拨款合计
        $tmp->setValue('z32', round($part_three_data['thisyear']['general_public_budget'], 2));
        // 基本支出
        $tmp->setValue('z33', round($part_three_data['thisyear']['jbzc'], 2));
        $tmp->setValue('z34', str_replace("-", "", round($part_three_data['calc']['v11'], 2)));
        $tmp->setValue('m34', $this->judge($part_three_data['calc']['v11']));
        $tmp->setValue('z35', str_replace("-", "", round($part_three_data['calc']['v12'], 2)));
        // 项目支出
        $tmp->setValue('z36', round($part_three_data['thisyear']['xmzc'], 2));
        $tmp->setValue('z37', str_replace("-", "", round($part_three_data['calc']['v14'], 2)));
        $tmp->setValue('m37', $this->judge($part_three_data['calc']['v14']));
        $tmp->setValue('z38', str_replace("-", "", round($part_three_data['calc']['v15'], 2)));
        // 一般公共预算
        $tmp->setValue('z39', round($part_three_data['thisyear']['shbzjyzc'], 2));
        $tmp->setValue('z40', str_replace("-", "", round($part_three_data['calc']['v19'], 2)));

        $tmp->setValue('z41', round($part_three_data['thisyear']['wsjkzc'], 2));
        $tmp->setValue('z42', str_replace("-", "", round($part_three_data['calc']['v20'], 2)));

        $tmp->setValue('z43', round($part_three_data['thisyear']['jnhbzc'], 2));
        $tmp->setValue('z44', str_replace("-", "", round($part_three_data['calc']['v21'], 2)));

        $tmp->setValue('z45', round($part_three_data['thisyear']['nlszc'], 2));
        $tmp->setValue('z46', str_replace("-", "", round($part_three_data['calc']['v22'], 2)));

        $tmp->setValue('z47', round($part_three_data['thisyear']['zrzyhyqxzc'], 2));
        $tmp->setValue('z48', str_replace("-", "", round($part_three_data['calc']['v23'], 2)));

        $tmp->setValue('z49', round($part_three_data['thisyear']['zfbzzc'], 2));
        $tmp->setValue('z50', str_replace("-", "", round($part_three_data['calc']['v24'], 2)));

        $tmp->setValue('z51', round($part_three_data['thisyear']['zhfzzc'], 2));
        $tmp->setValue('z52', str_replace("-", "", round($part_three_data['calc']['v25'], 2)));


        // 3-6
        $tmp->setValue('z95', round($part_three_data['thisyear']['jbzc'], 2));
        // 人员经费
        $tmp->setValue('z96', round($part_three_data['thisyear']['a15'], 2));
        // 公用经费
        $tmp->setValue('z97', round($part_three_data['thisyear']['a16'], 2));

        //3-7
        //项目列表
        //        var_dump(count($data['projectList']));exit;
        $people_array = [];
        $tmp->cloneBlock('PROJECTBLOCK', count($data['projectList']), true, true); //复制块
        foreach ($data['projectList'] as $k => $v) {
            $tmp->setValue('project_name#' . ($k + 1), ($k + 1) . '项目名称：' . $v['project_name']);
            $tmp->setValue('policy_basis#' . ($k + 1), isset($v['policy_basis']) ? $v['policy_basis'] : '');
            $tmp->setValue('budget_size#' . ($k + 1), isset($v['budget_size']) ? $v['budget_size'] : '');
            $tmp->setValue('responsible_unit#' . ($k + 1), $v['responsible_unit']);
            $tmp->setValue('fund_distribution#' . ($k + 1), $v['fund_distribution']);
            $tmp->setValue('fund_execution_tiem#' . ($k + 1), $v['fund_execution_tiem']);
            if ($v['type'] != 2) {
                $tmp->cloneBlock('PEOPLEBLOCK#' . ($k + 1), 0); //删除块
            } else {
                $tmp->cloneBlock('PEOPLEBLOCK#' . ($k + 1), 1, true); //显示块
                $people_array[$k + 1] = $v;
            }
        }
        foreach ($people_array as $people_k => $v) {
            $tmp->setValue('fund_source#' . ($people_k), $v['fund_source']);
            $tmp->setValue('subsidies_number#' . ($people_k), $v['subsidies_number']);
            $tmp->setValue('subsidies_standard#' . ($people_k), $v['subsidies_standard']);
            $tmp->setValue('subsidies_range#' . ($people_k), $v['subsidies_range']);
            $tmp->setValue('subsidies_way#' . ($people_k), $v['subsidies_way']);
            $tmp->setValue('distribution_program#' . ($people_k), $v['distribution_program']);
            $tmp->setValue('beneficiary_group#' . ($people_k), $v['beneficiary_group']);
        }
        //        $tmp->cloneBlock('PROJECTBLOCK', 2,true,true); //复制块
        //        $tmp->setValue('project_name#1', '1.项目名称：123');
        //        $tmp->setValue('policy_basis#1', '111');
        //        $tmp->setValue('budget_size#1', '11');
        //        $tmp->setValue('responsible_unit#1', 2);
        //        $tmp->setValue('fund_distribution#1',3);
        //        $tmp->setValue('fund_execution_tiem#1',4);
        //        $tmp->setValue('project_name#2', '2.项目名称：222');
        //        $tmp->setValue('policy_basis#2', '22222');
        //        $tmp->setValue('budget_size#2', '222222');
        //        $tmp->setValue('responsible_unit#2', '233333');
        //        $tmp->setValue('fund_distribution#2','2444444');
        //        $tmp->setValue('fund_execution_tiem#2','255555');

        // 3-8
        // 三公
        $tmp->setValue('z98', round($part_three_data['thisyear']['sgtotal'], 2));
        $tmp->setValue('z99', round($part_three_data['thisyear']['ygcg'], 2));
        $tmp->setValue('z100', round($part_three_data['thisyear']['gwycgz'], 2));
        $tmp->setValue('z101', round($part_three_data['thisyear']['gwycwh'], 2));
        $tmp->setValue('z102', round($part_three_data['thisyear']['gwjdf'], 2));

        $tmp->setValue('z103', str_replace("-", "", round($part_three_data['calc']['v54'], 2)));
        $tmp->setValue('m103', $this->judge($part_three_data['calc']['v54']));
        $tmp->setValue('z104', str_replace("-", "", round($part_three_data['calc']['v55'], 2)));
        $tmp->setValue('z105', str_replace("-", "", round($part_three_data['calc']['v56'], 2)));
        $tmp->setValue('m105', $this->judge($part_three_data['calc']['v56']));
        $tmp->setValue('z106', str_replace("-", "", round($part_three_data['calc']['v57'], 2)));

        $tmp->setValue('z107', round($part_three_data['thisyear']['gwycgz'], 2));
        $tmp->setValue('m107', $this->judge($part_three_data['thisyear']['gwycgz']));
        $tmp->setValue('z108', str_replace("-", "", round($part_three_data['calc']['v62'], 2)));

        $tmp->setValue('z109', round($part_three_data['thisyear']['gwycwh'], 2));
        $tmp->setValue('m109', $this->judge($part_three_data['thisyear']['gwycwh']));
        $tmp->setValue('z110', str_replace("-", "", round($part_three_data['calc']['v64'], 2)));

        $tmp->setValue('z111', round($part_three_data['thisyear']['gwjdf'], 2));
        $tmp->setValue('m111', $this->judge($part_three_data['thisyear']['gwjdf']));
        $tmp->setValue('z112', str_replace("-", "", round($part_three_data['calc']['v63'], 2)));

        // 3-9
        $tmp->setValue('z113', round($part_three_data['thisyear']['government_budgets'], 2));
        $tmp->setValue('z114', str_replace("-", "", round($part_three_data['calc']['v5'], 2)));
        $tmp->setValue('z115', str_replace("-", "", round($part_three_data['calc']['v6'], 2)));

        $tmp->setValue('z116', round($part_three_data['thisyear']['a17'], 2));
        $tmp->setValue('z117', str_replace("-", "", round($part_three_data['calc']['v58'], 2)));
        $tmp->setValue('m111', $this->judge($part_three_data['calc']['v58']));
        $tmp->setValue('z118', str_replace("-", "", round($part_three_data['calc']['v59'], 2)));

        // 3-10
        $tmp->setValue('z119', round($part_three_data['thisyear']['jgyxjf'], 2));
        $tmp->setValue('z120', str_replace("-", "", round($part_three_data['calc']['v60'], 2)));
        $tmp->setValue('m120', $this->judge($part_three_data['calc']['v60']));
        $tmp->setValue('z121', str_replace("-", "", round($part_three_data['calc']['v61'], 2)));
        // 34 37 69 72 78 84 87 90 103 109 120
        $tmp->setValue('u4', $this->judge2($part_three_data['calc']['v3']));
        $tmp->setValue('u35', $this->judge2($part_three_data['calc']['v12']));
        $tmp->setValue('u38', $this->judge2($part_three_data['calc']['v15']));
        $tmp->setValue('u70', $this->judge2($part_three_data['calc']['v37']));
        $tmp->setValue('u73', $this->judge2($part_three_data['calc']['v39']));
        $tmp->setValue('u79', $this->judge2($part_three_data['calc']['v43']));
        $tmp->setValue('u85', $this->judge2($part_three_data['calc']['v47']));
        $tmp->setValue('u88', $this->judge2($part_three_data['calc']['v49']));
        $tmp->setValue('u91', $this->judge2($part_three_data['calc']['v51']));
        $tmp->setValue('u104', $this->judge2($part_three_data['calc']['v55']));
        $tmp->setValue('u110', $this->judge2($part_three_data['calc']['v64']));
        $tmp->setValue('u121', $this->judge2($part_three_data['calc']['v61']));

        $tmp->setValue('u8', $this->judge2($part_three_data['calc']['v6']));
        $tmp->setValue('u12', $this->judge2($part_three_data['calc']['v9']));
        $tmp->setValue('u16', $this->judge2($part_three_data['calc']['v12']));
        $tmp->setValue('u20', $this->judge2($part_three_data['calc']['v15']));
        $tmp->setValue('u55', $this->judge2($part_three_data['calc']['v27']));
        $tmp->setValue('u58', $this->judge2($part_three_data['calc']['v29']));
        $tmp->setValue('u61', $this->judge2($part_three_data['calc']['v31']));
        $tmp->setValue('u64', $this->judge2($part_three_data['calc']['v33']));
        $tmp->setValue('u67', $this->judge2($part_three_data['calc']['v35']));
        $tmp->setValue('u76', $this->judge2($part_three_data['calc']['v41']));
        $tmp->setValue('u82', $this->judge2($part_three_data['calc']['v45']));
        $tmp->setValue('u94', $this->judge2($part_three_data['calc']['v52']));
        $tmp->setValue('u106', $this->judge2($part_three_data['calc']['v57']));
        $tmp->setValue('u108', $this->judge2($part_three_data['calc']['v62']));
        $tmp->setValue('u112', $this->judge2($part_three_data['calc']['v63']));
        $tmp->setValue('u115', $this->judge2($part_three_data['calc']['v6']));
        $tmp->setValue('u118', $this->judge2($part_three_data['calc']['v59']));
        // 原因
        $tmp->setValue('r1', $data['origin']['reason1']);
        $tmp->setValue('r2', $data['origin']['reason2']);
        $tmp->setValue('r3', $data['origin']['reason3']);
        $tmp->setValue('r4', $data['origin']['reason4']);
        $tmp->setValue('r5', $data['origin']['reason5']);
        // $tmp->setValue('r6', $data['origin']['reason6']);
        $tmp->setValue('r7', $data['origin']['reason7']);
        $tmp->setValue('r8', $data['origin']['reason8']);
        $tmp->setValue('r9', $data['origin']['reason9']);
        $tmp->setValue('r10', $data['origin']['reason10']);
        $tmp->setValue('r11', $data['origin']['reason11']);
        $tmp->setValue('r12', $data['origin']['reason12']);
        $tmp->setValue('r13', $data['origin']['reason13']);
        $tmp->setValue('r14', $data['origin']['reason14']);
        $tmp->setValue('r15', $data['origin']['reason15']);
        $tmp->setValue('r16', $data['origin']['reason16']);
        $tmp->setValue('r17', $data['origin']['reason17']);
        $tmp->setValue('r18', $data['origin']['reason18']);
        $tmp->setValue('r19', $data['origin']['reason19']);
        $tmp->setValue('r20', $data['origin']['reason20']);
        $tmp->setValue('r21', $data['origin']['reason21']);
        $tmp->setValue('r22', $data['origin']['reason22']);
        $tmp->setValue('r23', $data['origin']['reason23']);
        $tmp->setValue('r24', $data['origin']['reason24']);
        $tmp->setValue('r25', $data['origin']['reason25']);
        $tmp->setValue('r26', $data['origin']['reason26']);
        $tmp->setValue('r27', $data['origin']['reason27']);
        $tmp->setValue('r28', $data['origin']['reason28']);
        $tmp->setValue('r29', $data['origin']['reason29']);
        $tmp->setValue('r30', $data['origin']['reason30']);

        $tmp->setValue('j1', round($data['origin']['ra1'], 2));
        $tmp->setValue('j2', round($data['origin']['ra2'], 2));
        $tmp->setValue('j3', round($data['origin']['ra3'], 2));
        $tmp->setValue('j4', round($data['origin']['ra4'], 2));
        $tmp->setValue('j5', round($data['origin']['ra5'], 2));
        $tmp->setValue('j6', round($data['origin']['ra6'], 2));
        $tmp->setValue('j7', round($data['origin']['ra7'], 2));
        $tmp->setValue('j8', round($data['origin']['ra8'], 2));
        $tmp->setValue('j9', round($data['origin']['ra9'], 2));
        $tmp->setValue('j10', round($data['origin']['ra10'], 2));
        $tmp->setValue('j11', round($data['origin']['ra11'], 2));
        $tmp->setValue('j12', round($data['origin']['ra12'], 2));
        $tmp->setValue('j13', round($data['origin']['ra13']));
        $tmp->setValue('j14', round($data['origin']['ra14'], 2));
        $tmp->setValue('j15', round($data['origin']['ra15'], 2));
        $tmp->setValue('j16', round($data['origin']['ra16'], 2));
        $tmp->setValue('j17', round($data['origin']['ra17'], 2));
        $tmp->setValue('j18', round($data['origin']['ra18'], 2));
        $tmp->setValue('j19', round($data['origin']['ra19']));
        $tmp->setValue('j20', round($data['origin']['ra20']));
        $tmp->setValue('j21', round($data['origin']['ra21']));
        $tmp->setValue('j22', round($data['origin']['ra22']));
        $tmp->setValue('j23', round($data['origin']['ra23']));

        // new part 3
        $reasonjson = json_decode($data['origin']['reasonset'], true);
        $newpart3 = $this->part32($code);
        $np1 = $this->nptmp($part_three_data['others3']);
        $tmp->setValue('np1', $np1);
        $np2 = $this->nptmp($newpart3['zcbudget2']);
        $tmp->setValue('np2', $np2);
        // zcbudget
        $np3 = $this->tmp3($newpart3['zcbudget'], $data['origin']['reasonset']);
        $tmp->setValue('np3', $np3);
        $np4 = $this->tmp4($newpart3['zcbudget']);
        $tmp->setValue('np4', $np4);
        $np5 = $this->tmp5($newpart3['lists'], $reasonjson);
        $tmp->setValue('np5', $np5);



        $file_name = root_path() . 'public/uploads/' . date('Y') . '年度' . $data['obj']['district_name'] . $data['obj']['institutions_name'] . '单位预算公开.docx';
        $tmp->saveAs($file_name); //另存为

        return ['file' => download($file_name), 'name' => $file_name];
    }

    public function getPartThreeData($code)
    {
        $dis_code = substr($code, 0, 6);
        $ins_code = substr($code, 6, 6);
        $thisyears = Db::query("select * from admin_budget where type = 1 and institutions_code = :code1 and district_code = :code2", ['code1' => $ins_code, 'code2' => $dis_code]);
        $lastyears = Db::query("select * from admin_budget where type = 2 and institutions_code = :code1 and district_code = :code2", ['code1' => $ins_code, 'code2' => $dis_code]);
        // var_dump($thisyears);exit;
        $thisyear = [];
        $lastyear = [];
        $calc = [];

        // 第三部分

        // 一般公共预算基本支出
        // 人员经费
        $ryarr = [30101, 30102, 30102, 30107, 30108, 30110, 30111, 30112, 30113, 30199, 30302, 30305, 30309, 30399];
        // 公用经费
        $gyarr = [30201, 30208, 30211, 30231];


        $total = 0;
        $general_public_budget = 0;
        $government_budgets = 0;
        $institution_other_income = 0;
        $gov_capital = 0;
        $utility_income = 0;
        $institution_management_income = 0;
        $jbzc = 0; // 基本支出
        $xmzc = 0; // 项目支出
        $fiscal_appropriation = 0;
        $shbzjyzc = 0;
        $wsjkzc = 0;
        $jnhbzc = 0;
        $cxsqzc = 0;
        $nlszc = 0;
        $zrzyhyqxzc = 0;
        $zfbzzc = 0;
        $zhfzzc = 0;
        $ybggfwzc = 0;
        $qtzc = 0;
        $a1 = 0;
        $a2 = 0;
        $a3 = 0;
        $a4 = 0;
        $a5 = 0;
        $a6 = 0;
        $a7 = 0;
        $a8 = 0;
        $a9 = 0;
        $a10 = 0;
        $a11 = 0;
        $a12 = 0;
        $a13 = 0;
        $a14 = 0;
        $a15 = 0;
        $a16 = 0;
        $ygcg = 0;
        $gwycgz = 0;
        $gwycwh = 0;
        $gwjdf = 0;
        $a17 = 0;
        $jgyxjf = 0;
        $project_count = 0;
        $project_payment = 0;

        $in_project = [];
        $ryinclude = [];
        $gyinclude = [];

        $ybggfwzc_arr = [];

        $jbzc1 = 0;
        $xmzc1 = 0;

        foreach ($thisyears as $item) {
            // if ($item['type'] == 2) continue;
            $total += $item['total'];
            $general_public_budget += $item['general_public_budget'];
            $government_budgets += $item['government_budgets'];
            $gov_capital += $item['government_capital_management_budget'];
            $institution_other_income += $item['institution_other_income'];
            $fiscal_appropriation += $item['fiscal_appropriation'];
            $utility_income += $item['utility_income'];
            $institution_management_income += $item['institution_management_income'];

            $jbzc += $item['project_category_name'] == "基本支出" ? $item['total'] : 0;
            $xmzc += $item['project_category_name'] == "项目支出" ? $item['total'] : 0;
            $jbzc1 += $item['project_category_name'] == "基本支出" ? $item['general_public_budget'] : 0;
            $xmzc1 += $item['project_category_name'] == "项目支出" ? $item['general_public_budget'] : 0;

            $shbzjyzc += $item['functional_category_name_class'] == "社会保障和就业支出" ? $item['total'] : 0;
            $wsjkzc += $item['functional_category_name_class'] == "卫生健康支出" ? $item['total'] : 0;
            $jnhbzc += $item['functional_category_name_class'] == "节能环保支出" ? $item['total'] : 0;
            $cxsqzc += $item['functional_category_name_class'] == "城乡社区支出" ? $item['total'] : 0;
            $nlszc += $item['functional_category_name_class'] == "农林水支出" ? $item['total'] : 0;
            $zrzyhyqxzc += $item['functional_category_name_class'] == "自然资源海洋气象等支出" ? $item['total'] : 0;
            $zfbzzc += $item['functional_category_name_class'] == "住房保障支出" ? $item['total'] : 0;
            $zhfzzc += $item['functional_category_name_class'] == "灾害防治及应急管理支出" ? $item['total'] : 0;
            $ybggfwzc += $item['functional_category_name_class'] == "一般公共服务支出" ? $item['total'] : 0;
            $qtzc += $item['functional_category_name_class'] == "其他支出" ? $item['total'] : 0;

            // 一般公共服务支出项
            if ($item['functional_category_code_class'] == 213) {
                $_key = 'k' . $item['functional_category_name_class'] . '_' . $item['functional_category_name_fund'] . '_' . $item['functional_category_name_item'];
                $ybggfwzc_arr[$_key] = 1;
            }

            if ($item['functional_category_code_class'] == 208 && $item['functional_category_code_fund'] == 5) {
                if ($item['functional_category_code_item'] == 1) {
                    $a1 += $item['general_public_budget'];
                }
                if ($item['functional_category_code_item'] == 5) {
                    $a2 += $item['general_public_budget'];
                }
            }

            if ($item['functional_category_code_class'] == 210 && $item['functional_category_code_fund'] == 11) {
                if ($item['functional_category_code_item'] == 1) {
                    $a3 += $item['general_public_budget'];
                }
                if ($item['functional_category_code_item'] == 3) {
                    $a4 += $item['general_public_budget'];
                }
            }
            if ($item['functional_category_code_class'] == 211 && $item['functional_category_code_fund'] == 4) {
                if ($item['functional_category_code_item'] == 1) {
                    $a5 += $item['general_public_budget'];
                }
            }
            if ($item['functional_category_code_class'] == 211 && $item['functional_category_code_fund'] == 6) {
                if ($item['functional_category_code_item'] == 99) {
                    $a6 += $item['general_public_budget'];
                }
            }

            if ($item['functional_category_code_class'] == 213 && $item['functional_category_code_fund'] == 2) {
                if ($item['functional_category_code_item'] == 5) {
                    $a7 += $item['general_public_budget'];
                }
                if ($item['functional_category_code_item'] == 99) {
                    $a8 += $item['general_public_budget'];
                }
            }
            if ($item['functional_category_code_class'] == 220 && $item['functional_category_code_fund'] == 1) {
                if ($item['functional_category_code_item'] == 1) {
                    $a9 += $item['general_public_budget'];
                }
                if ($item['functional_category_code_item'] == 6) {
                    $a10 += $item['general_public_budget'];
                }
                if ($item['functional_category_code_item'] == 50) {
                    $a11 += $item['general_public_budget'];
                }
                if ($item['functional_category_code_item'] == 99) {
                    $a12 += $item['general_public_budget'];
                }
            }
            if ($item['functional_category_code_class'] == 221 && $item['functional_category_code_fund'] == 2) {
                if ($item['functional_category_code_item'] == 1) {
                    $a13 += $item['general_public_budget'];
                }
            }

            if ($item['functional_category_code_class'] == 224 && $item['functional_category_code_fund'] == 1) {
                if ($item['functional_category_code_item'] == 4) {
                    $a14 += $item['general_public_budget'];
                }
            }
            if (in_array($item['economics_category_code'], $ryarr)) {
                $a15 += $item['general_public_budget'];
                if (!in_array($item['economics_category_name'], $ryinclude) && $item['general_public_budget'] > 0) {
                    array_push($ryinclude, $item['economics_category_name']);
                }
            }
            if (in_array($item['economics_category_code'], $gyarr)) {
                $a16 += $item['general_public_budget'];
                if (!in_array($item['economics_category_name'], $gyinclude) && $item['general_public_budget'] > 0) {
                    array_push($gyinclude, $item['economics_category_name']);
                }
            }
            if ($item['economics_category_code'] == "30212") {
                $ygcg += $item['general_public_budget'];
            }
            if (in_array($item['economics_category_code'], [30913, 31013])) {
                $gwycgz += $item['general_public_budget'];
            }
            if ($item['economics_category_code'] == "30231") {
                $gwycwh += $item['general_public_budget'];
            }
            if ($item['economics_category_code'] == "30217") {
                $gwjdf += $item['general_public_budget'];
            }
            if ($item['functional_category_code_class'] == 212 && $item['functional_category_code_fund'] == 8) {
                if ($item['functional_category_code_item'] == 16) {
                    $a17 += $item['general_public_budget'];
                }
            }
            // 公式： 项目类别 - 基本支出  && 经济分类编码 302
            if ($item['economics_category_code_class'] == "302" && $item['project_category_name'] == "基本支出") {
                $jgyxjf += $item['fiscal_appropriation'];
            }
            if ($item['project_category_name'] == "项目支出" && $item['fiscal_appropriation'] > 0) {
                // 重复的project_code 算一个项目
                if (!in_array($item['project_code'], $in_project)) {
                    $project_count += 1;
                    array_push($in_project, $item['project_code']);
                }

                $project_payment += $item['fiscal_appropriation'];
            }
        }
        $thisyear['total'] = $total;
        $thisyear['general_public_budget'] = $general_public_budget;
        $thisyear['government_budgets'] = $government_budgets;
        $thisyear['institution_other_income'] = $institution_other_income;
        $thisyear['fiscal_appropriation'] = $fiscal_appropriation;
        $thisyear['gov_capital'] = $gov_capital;
        $thisyear['utility_income'] = $utility_income;
        $thisyear['institution_management_income'] = $institution_management_income;
        $thisyear['jbzc'] = $jbzc;
        $thisyear['xmzc'] = $xmzc;
        $thisyear['jbzc1'] = $jbzc1;
        $thisyear['xmzc1'] = $xmzc1;
        $thisyear['shbzjyzc'] = $shbzjyzc;
        $thisyear['wsjkzc'] = $wsjkzc;
        $thisyear['jnhbzc'] = $jnhbzc;
        $thisyear['cxsqzc'] = $cxsqzc;
        $thisyear['nlszc'] = $nlszc;
        $thisyear['zrzyhyqxzc'] = $zrzyhyqxzc;
        $thisyear['zfbzzc'] = $zfbzzc;
        $thisyear['zhfzzc'] = $zhfzzc;
        $thisyear['ybggfwzc'] = $ybggfwzc;
        $thisyear['qtzc'] = $qtzc;
        $thisyear['a1'] = $a1;
        $thisyear['a2'] = $a2;
        $thisyear['a3'] = $a3;
        $thisyear['a4'] = $a4;
        $thisyear['a5'] = $a5;
        $thisyear['a6'] = $a6;
        $thisyear['a7'] = $a7;
        $thisyear['a8'] = $a8;
        $thisyear['a9'] = $a9;
        $thisyear['a10'] = $a10;
        $thisyear['a11'] = $a11;
        $thisyear['a12'] = $a12;
        $thisyear['a13'] = $a13;
        $thisyear['a14'] = $a14;
        $thisyear['a15'] = $a15;
        $thisyear['a16'] = $a16;
        $thisyear['a17'] = $a17;
        $thisyear['ygcg'] = $ygcg;
        $thisyear['gwycgz'] = $gwycgz;
        $thisyear['gwycwh'] = $gwycwh;
        $thisyear['gwjdf'] = $gwjdf;
        $thisyear['sgtotal'] = $ygcg + $gwycgz + $gwycwh + $gwjdf;
        $thisyear['jgyxjf'] = $jgyxjf;
        $thisyear['project_count'] = $project_count;
        $thisyear['project_payment'] = $project_payment;
        $thisyear['ryinclude'] = implode("、", $ryinclude);
        $thisyear['gyinclude'] = implode("、", $gyinclude);
        $thisyear['ybggfwzc_arr'] = $ybggfwzc_arr;


        $total = 0;
        $general_public_budget = 0;
        $government_budgets = 0;
        $gov_capital = 0;
        $institution_other_income = 0;
        $utility_income = 0;
        $institution_management_income = 0;
        $jbzc = 0;
        $xmzc = 0;
        $fiscal_appropriation = 0;
        $shbzjyzc = 0;
        $wsjkzc = 0;
        $jnhbzc = 0;
        $cxsqzc = 0;
        $nlszc = 0;
        $zrzyhyqxzc = 0;
        $zfbzzc = 0;
        $zhfzzc = 0;
        $ybggfwzc = 0;
        $qtzc = 0;
        $a1 = 0;
        $a2 = 0;
        $a3 = 0;
        $a4 = 0;
        $a5 = 0;
        $a6 = 0;
        $a7 = 0;
        $a8 = 0;
        $a9 = 0;
        $a10 = 0;
        $a11 = 0;
        $a12 = 0;
        $a13 = 0;
        $a14 = 0;
        $a17 = 0;
        $ygcg = 0;
        $gwycgz = 0;
        $gwycwh = 0;
        $gwjdf = 0;
        $jgyxjf = 0;
        $jbzc1 = 0;
        $xmzc1 = 0;

        foreach ($lastyears as $item) {
            // if ($item['type'] == 1) continue;
            $total += $item['total'];
            $general_public_budget += $item['general_public_budget'];
            $government_budgets += $item['government_budgets'];
            $gov_capital += $item['government_capital_management_budget'];
            $institution_other_income += $item['institution_other_income'];
            $fiscal_appropriation += $item['fiscal_appropriation'];
            $utility_income += $item['utility_income'];
            $institution_management_income += $item['institution_management_income'];

            $jbzc += $item['project_category_name'] == "基本支出" ? $item['total'] : 0;
            $xmzc += $item['project_category_name'] == "项目支出" ? $item['total'] : 0;

            $jbzc1 += $item['project_category_name'] == "基本支出" ? $item['general_public_budget'] : 0;
            $xmzc1 += $item['project_category_name'] == "项目支出" ? $item['general_public_budget'] : 0;

            $shbzjyzc += $item['functional_category_name_class'] == "社会保障和就业支出" ? $item['total'] : 0;
            $wsjkzc += $item['functional_category_name_class'] == "卫生健康支出" ? $item['total'] : 0;
            $jnhbzc += $item['functional_category_name_class'] == "节能环保支出" ? $item['total'] : 0;
            $cxsqzc += $item['functional_category_name_class'] == "城乡社区支出" ? $item['total'] : 0;
            $nlszc += $item['functional_category_name_class'] == "农林水支出" ? $item['total'] : 0;
            $zrzyhyqxzc += $item['functional_category_name_class'] == "自然资源海洋气象等支出" ? $item['total'] : 0;
            $zfbzzc += $item['functional_category_name_class'] == "住房保障支出" ? $item['total'] : 0;
            $zhfzzc += $item['functional_category_name_class'] == "灾害防治及应急管理支出" ? $item['total'] : 0;
            $ybggfwzc += $item['functional_category_name_class'] == "一般公共服务支出" ? $item['total'] : 0;
            $qtzc += $item['functional_category_name_class'] == "其他支出" ? $item['total'] : 0;

            if ($item['functional_category_code_class'] == 208 && $item['functional_category_code_fund'] == 5) {
                if ($item['functional_category_code_item'] == 1) {
                    $a1 += $item['general_public_budget'];
                }
                if ($item['functional_category_code_item'] == 5) {
                    $a2 += $item['general_public_budget'];
                }
            }
            if ($item['functional_category_code_class'] == 210 && $item['functional_category_code_fund'] == 11) {
                if ($item['functional_category_code_item'] == 1) {
                    $a3 += $item['general_public_budget'];
                }
                if ($item['functional_category_code_item'] == 3) {
                    $a4 += $item['general_public_budget'];
                }
            }
            if ($item['functional_category_code_class'] == 211 && $item['functional_category_code_fund'] == 4) {
                if ($item['functional_category_code_item'] == 1) {
                    $a5 += $item['general_public_budget'];
                }
            }
            if ($item['functional_category_code_class'] == 211 && $item['functional_category_code_fund'] == 6) {
                if ($item['functional_category_code_item'] == 99) {
                    $a6 += $item['general_public_budget'];
                }
            }

            if ($item['functional_category_code_class'] == 213 && $item['functional_category_code_fund'] == 2) {
                if ($item['functional_category_code_item'] == 5) {
                    $a7 += $item['general_public_budget'];
                }
                if ($item['functional_category_code_item'] == 99) {
                    $a8 += $item['general_public_budget'];
                }
            }
            if ($item['functional_category_code_class'] == 220 && $item['functional_category_code_fund'] == 1) {
                if ($item['functional_category_code_item'] == 1) {
                    $a9 += $item['general_public_budget'];
                }
                if ($item['functional_category_code_item'] == 6) {
                    $a10 += $item['general_public_budget'];
                }
                if ($item['functional_category_code_item'] == 50) {
                    $a11 += $item['general_public_budget'];
                }
                if ($item['functional_category_code_item'] == 99) {
                    $a12 += $item['general_public_budget'];
                }
            }
            if ($item['functional_category_code_class'] == 221 && $item['functional_category_code_fund'] == 2) {
                if ($item['functional_category_code_item'] == 1) {
                    $a13 += $item['general_public_budget'];
                }
            }

            if ($item['functional_category_code_class'] == 224 && $item['functional_category_code_fund'] == 1) {
                if ($item['functional_category_code_item'] == 4) {
                    $a14 += $item['general_public_budget'];
                }
            }
            if ($item['economics_category_code'] == "30212") {
                $ygcg += $item['general_public_budget'];
            }
            if (in_array($item['economics_category_code'], [30913, 31013])) {
                $gwycgz += $item['general_public_budget'];
            }
            if ($item['economics_category_code'] == "30231") {
                $gwycwh += $item['general_public_budget'];
            }
            if ($item['economics_category_code'] == "30217") {
                $gwjdf += $item['general_public_budget'];
            }
            if ($item['functional_category_code_class'] == 212 && $item['functional_category_code_fund'] == 8) {
                if ($item['functional_category_code_item'] == 16) {
                    $a17 += $item['general_public_budget'];
                }
            }
            // if ($item['economics_category_code_class'] == "302") {
            //     $jgyxjf += $item['fiscal_appropriation'];
            // }
            // 公式： 项目类别 - 基本支出  && 经济分类编码 302
            if ($item['economics_category_code_class'] == "302" && $item['project_category_name'] == "基本支出") {
                $jgyxjf += $item['fiscal_appropriation'];
            }
        }
        $lastyear['total'] = $total;
        $lastyear['general_public_budget'] = $general_public_budget;
        $lastyear['government_budgets'] = $government_budgets;
        $lastyear['gov_capital'] = $gov_capital;
        $lastyear['institution_other_income'] = $institution_other_income;
        $lastyear['fiscal_appropriation'] = $fiscal_appropriation;
        $lastyear['utility_income'] = $utility_income;
        $lastyear['institution_management_income'] = $institution_management_income;
        $lastyear['jbzc'] = $jbzc;
        $lastyear['xmzc'] = $xmzc;
        $lastyear['jbzc1'] = $jbzc1;
        $lastyear['xmzc1'] = $xmzc1;
        $lastyear['shbzjyzc'] = $shbzjyzc;
        $lastyear['wsjkzc'] = $wsjkzc;
        $lastyear['jnhbzc'] = $jnhbzc;
        $lastyear['cxsqzc'] = $cxsqzc;
        $lastyear['nlszc'] = $nlszc;
        $lastyear['zrzyhyqxzc'] = $zrzyhyqxzc;
        $lastyear['zfbzzc'] = $zfbzzc;
        $lastyear['zhfzzc'] = $zhfzzc;
        $lastyear['ybggfwzc'] = $ybggfwzc;
        $lastyear['qtzc'] = $qtzc;
        $lastyear['a1'] = $a1;
        $lastyear['a2'] = $a2;
        $lastyear['a3'] = $a3;
        $lastyear['a4'] = $a4;
        $lastyear['a5'] = $a5;
        $lastyear['a6'] = $a6;
        $lastyear['a7'] = $a7;
        $lastyear['a8'] = $a8;
        $lastyear['a9'] = $a9;
        $lastyear['a10'] = $a10;
        $lastyear['a11'] = $a11;
        $lastyear['a12'] = $a12;
        $lastyear['a13'] = $a13;
        $lastyear['a14'] = $a14;
        $lastyear['a17'] = $a17;
        $lastyear['ygcg'] = $ygcg;
        $lastyear['gwycgz'] = $gwycgz;
        $lastyear['gwycwh'] = $gwycwh;
        $lastyear['gwjdf'] = $gwjdf;
        $lastyear['sgtotal'] = $ygcg + $gwycgz + $gwycwh + $gwjdf;
        $lastyear['jgyxjf'] = $jgyxjf;

        // 标题二
        $calc['v1'] = $thisyear['total'] == 0 ? 100 : ($thisyear['general_public_budget'] / $thisyear['total']) * 100;
        $calc['v2'] = $thisyear['general_public_budget'] - $lastyear['general_public_budget'];
        // $calc['v3'] = $thisyear['general_public_budget'] == 0 ? 0 : 100 * (($thisyear['general_public_budget'] - $lastyear['general_public_budget']) / $thisyear['general_public_budget']);
        $calc['v3'] = $lastyear['general_public_budget'] == 0 ? 100 : (($thisyear['general_public_budget'] - $lastyear['general_public_budget']) / $lastyear['general_public_budget']) * 100;

        $calc['v4'] = $thisyear['total'] == 0 ? 100 : ($thisyear['government_budgets'] / $thisyear['total']) * 100;
        $calc['v5'] = $thisyear['government_budgets'] - $lastyear['government_budgets'];
        // $calc['v6'] = $thisyear['government_budgets'] == 0 ? 0 : 100 * (($thisyear['government_budgets'] - $lastyear['government_budgets']) / $thisyear['government_budgets']);
        $calc['v6'] = $lastyear['government_budgets'] == 0 ? 100 : (($thisyear['government_budgets'] - $lastyear['government_budgets']) / $lastyear['government_budgets']) * 100;

        $calc['v7'] = $thisyear['total'] == 0 ? 100 : ($thisyear['institution_other_income'] / $thisyear['total']) * 100;
        $calc['v8'] = $thisyear['institution_other_income'] - $lastyear['institution_other_income'];
        // $calc['v9'] = $thisyear['institution_other_income'] == 0 ? 0 : 100 * (($thisyear['institution_other_income'] - $lastyear['institution_other_income']) / $thisyear['institution_other_income']);
        $calc['v9'] = $lastyear['institution_other_income'] == 0 ? 100 : (($thisyear['institution_other_income'] - $lastyear['institution_other_income']) / $lastyear['institution_other_income']) * 100;
        // 标题三
        $calc['v10'] = $thisyear['total'] == 0 ? 100 : ($thisyear['jbzc'] / $thisyear['total']) * 100;
        $calc['v11'] = $thisyear['jbzc'] - $lastyear['jbzc'];
        $calc['c11'] = $this->calc1($thisyear['jbzc1'], $lastyear['jbzc1']);
        // $calc['v12'] = $thisyear['jbzc'] == 0 ? 0 : 100 * (($thisyear['jbzc'] - $lastyear['jbzc']) / $thisyear['jbzc']);
        // $calc['v12'] = $lastyear['jbzc'] == 0 ? 100 : (($thisyear['jbzc'] - $lastyear['jbzc']) / $lastyear['jbzc']) * 100;
        // $calc['v12'] =  $thisyear['xmzc'] == 0 ? 100 : 100 * ($thisyear['jbzc'] / ($thisyear['jbzc'] + $thisyear['xmzc']));
        $calc['v12'] =  $lastyear['jbzc'] == 0 ? 100 : 100 * (($thisyear['jbzc'] - $lastyear['jbzc']) / $lastyear['jbzc']);
        $calc['c12'] =  $this->calc2($thisyear['jbzc1'], $lastyear['jbzc1']);

        $calc['v13'] = $thisyear['total'] == 0 ? 100 : ($thisyear['xmzc'] / $thisyear['total']) * 100;
        $calc['v14'] = $thisyear['xmzc'] - $lastyear['xmzc'];
        $calc['c14'] = $this->calc1($thisyear['xmzc1'], $lastyear['xmzc1']);
        // $calc['v15'] = $thisyear['xmzc'] == 0 ? 0 : 100 * (($thisyear['xmzc'] - $lastyear['xmzc']) / $thisyear['xmzc']);
        // $calc['v15'] = $lastyear['xmzc'] == 0 ? 100 : (($thisyear['xmzc'] - $lastyear['xmzc']) / $lastyear['xmzc']) * 100;
        // var_dump($thisyear['jbzc'], $thisyear['xmzc']);
        // $calc['v15'] = 100 * ($thisyear['xmzc'] / ($thisyear['jbzc'] + $thisyear['xmzc']));
        $calc['v15'] =  $lastyear['xmzc'] == 0 ? 100 : 100 * (($thisyear['xmzc'] - $lastyear['xmzc']) / $lastyear['xmzc']);
        $calc['c15'] =  $this->calc2($thisyear['xmzc1'], $lastyear['xmzc1']);

        // 标题四

        // 标题五
        // $calc['v15'] = $thisyear['jbzc'] - $lastyear['jbzc'];
        $calc['v16'] = $thisyear['jbzc'] == 0 ? 100 : 100 * (($thisyear['jbzc'] - $lastyear['jbzc']) / $thisyear['jbzc']);
        $calc['v17'] = $thisyear['xmzc'] - $lastyear['xmzc'];
        $calc['v18'] = $thisyear['xmzc'] == 0 ? 100 : 100 * (($thisyear['xmzc'] - $lastyear['xmzc']) / $thisyear['xmzc']);

        $calc['v19'] = $thisyear['total'] == 0 ? 100 : ($thisyear['shbzjyzc'] / $thisyear['total']) * 100;
        $calc['v20'] = $thisyear['total'] == 0 ? 100 : ($thisyear['wsjkzc'] / $thisyear['total']) * 100;
        $calc['v21'] = $thisyear['total'] == 0 ? 100 : ($thisyear['jnhbzc'] / $thisyear['total']) * 100;
        $calc['v22'] = $thisyear['total'] == 0 ? 100 : ($thisyear['nlszc'] / $thisyear['total']) * 100;
        $calc['v23'] = $thisyear['total'] == 0 ? 100 : ($thisyear['zrzyhyqxzc'] / $thisyear['total']) * 100;
        $calc['v24'] = $thisyear['total'] == 0 ? 100 : ($thisyear['zfbzzc'] / $thisyear['total']) * 100;
        $calc['v25'] = $thisyear['total'] == 0 ? 100 : ($thisyear['zhfzzc'] / $thisyear['total']) * 100;
        $calc['f4'] = $thisyear['total'] == 0 ? 100 : ($thisyear['ybggfwzc'] / $thisyear['total']) * 100;

        $calc['v26'] = $thisyear['a1'] - $lastyear['a1'];
        // $calc['v27'] = $thisyear['a1'] == 0 ? 0 : (($thisyear['a1'] - $lastyear['a1']) / $thisyear['a1']) * 100;
        $calc['v27'] = $lastyear['a1'] == 0 ? 100 : 100 * (($thisyear['a1'] - $lastyear['a1']) / $lastyear['a1']);

        $calc['v28'] = $thisyear['a2'] - $lastyear['a2'];
        // $calc['v29'] = $thisyear['a2'] == 0 ? 0 : (($thisyear['a2'] - $lastyear['a2']) / $thisyear['a2']) * 100;
        $calc['v29'] = $lastyear['a2'] == 0 ? 100 : 100 * (($thisyear['a2'] - $lastyear['a2']) / $lastyear['a2']);

        $calc['v30'] = $thisyear['a3'] - $lastyear['a3'];
        // $calc['v31'] = $thisyear['a3'] == 0 ? 0 : (($thisyear['a3'] - $lastyear['a3']) / $thisyear['a3']) * 100;
        $calc['v31'] = $lastyear['a3'] == 0 ? 100 : 100 * (($thisyear['a3'] - $lastyear['a3']) / $lastyear['a3']);

        $calc['v32'] = $thisyear['a4'] - $lastyear['a4'];
        // $calc['v33'] = $thisyear['a4'] == 0 ? 0 : (($thisyear['a4'] - $lastyear['a4']) / $thisyear['a4']) * 100;
        $calc['v33'] = $lastyear['a4'] == 0 ? 100 : 100 * (($thisyear['a4'] - $lastyear['a4']) / $lastyear['a4']);

        $calc['v34'] = $thisyear['a5'] - $lastyear['a5'];
        // $calc['v35'] = $thisyear['a5'] == 0 ? 0 : (($thisyear['a5'] - $lastyear['a5']) / $thisyear['a5']) * 100;
        $calc['v35'] = $lastyear['a5'] == 0 ? 100 : 100 * (($thisyear['a5'] - $lastyear['a5']) / $lastyear['a5']);

        $calc['v36'] = $thisyear['a6'] - $lastyear['a6'];
        // $calc['v37'] = $thisyear['a6'] == 0 ? 0 : (($thisyear['a6'] - $lastyear['a6']) / $thisyear['a6']) * 100;
        $calc['v37'] = $lastyear['a6'] == 0 ? 100 : 100 * (($thisyear['a6'] - $lastyear['a6']) / $lastyear['a6']);

        $calc['v38'] = $thisyear['a7'] - $lastyear['a7'];
        // $calc['v39'] = $thisyear['a7'] == 0 ? 0 : (($thisyear['a7'] - $lastyear['a7']) / $thisyear['a7']) * 100;
        $calc['v39'] = $lastyear['a7'] == 0 ? 100 : 100 * (($thisyear['a7'] - $lastyear['a7']) / $lastyear['a7']);

        $calc['v40'] = $thisyear['a8'] - $lastyear['a8'];
        // $calc['v41'] = $thisyear['a8'] == 0 ? 0 : (($thisyear['a8'] - $lastyear['a8']) / $thisyear['a8']) * 100;
        $calc['v41'] = $lastyear['a8'] == 0 ? 100 : 100 * (($thisyear['a8'] - $lastyear['a8']) / $lastyear['a8']);

        $calc['v42'] = $thisyear['a9'] - $lastyear['a9'];
        // $calc['v43'] = $thisyear['a9'] == 0 ? 0 : (($thisyear['a9'] - $lastyear['a9']) / $thisyear['a9']) * 100;
        $calc['v43'] = $lastyear['a9'] == 0 ? 100 : 100 * (($thisyear['a9'] - $lastyear['a9']) / $lastyear['a9']);

        $calc['v44'] = $thisyear['a10'] - $lastyear['a10'];
        // $calc['v45'] = $thisyear['a10'] == 0 ? 0 : (($thisyear['a10'] - $lastyear['a10']) / $thisyear['a10']) * 100;
        $calc['v45'] = $lastyear['a10'] == 0 ? 100 : 100 * (($thisyear['a10'] - $lastyear['a10']) / $lastyear['a10']);

        $calc['v46'] = $thisyear['a11'] - $lastyear['a11'];
        // $calc['v47'] = $thisyear['a11'] == 0 ? 0 : (($thisyear['a11'] - $lastyear['a11']) / $thisyear['a11']) * 100;
        $calc['v47'] = $lastyear['a11'] == 0 ? 100 : 100 * (($thisyear['a11'] - $lastyear['a11']) / $lastyear['a11']);

        $calc['v48'] = $thisyear['a12'] - $lastyear['a12'];
        // $calc['v49'] = $thisyear['a12'] == 0 ? 0 : (($thisyear['a12'] - $lastyear['a12']) / $thisyear['a12']) * 100;
        $calc['v49'] = $lastyear['a12'] == 0 ? 100 : 100 * (($thisyear['a12'] - $lastyear['a12']) / $lastyear['a12']);

        $calc['v50'] = $thisyear['a13'] - $lastyear['a13'];
        // $calc['v51'] = $thisyear['a13'] == 0 ? 0 : (($thisyear['a13'] - $lastyear['a13']) / $thisyear['a13']) * 100;
        $calc['v51'] = $lastyear['a13'] == 0 ? 100 : 100 * (($thisyear['a13'] - $lastyear['a13']) / $lastyear['a13']);

        $calc['v52'] = $thisyear['a14'] - $lastyear['a14'];
        // $calc['v53'] = $thisyear['a14'] == 0 ? 0 : (($thisyear['a14'] - $lastyear['a14']) / $thisyear['a14']) * 100;
        $calc['v53'] = $lastyear['a14'] == 0 ? 100 : 100 * (($thisyear['a14'] - $lastyear['a14']) / $lastyear['a14']);

        $calc['v54'] = $thisyear['sgtotal'] - $lastyear['sgtotal'];
        // $calc['v55'] = $thisyear['sgtotal'] == 0 ? 0 : (($thisyear['sgtotal'] - $lastyear['sgtotal']) / $thisyear['sgtotal']) * 100;
        $calc['v55'] = $lastyear['sgtotal'] == 0 ? 100 : (($thisyear['sgtotal'] - $lastyear['sgtotal']) / $lastyear['sgtotal']) * 100;
        $calc['c55'] = $this->calc2($thisyear['sgtotal'], $lastyear['sgtotal']);

        $calc['v56'] = $thisyear['ygcg'] - $lastyear['ygcg'];
        $calc['v57'] = $lastyear['ygcg'] == 0 ? 100 : (($thisyear['ygcg'] - $lastyear['ygcg']) / $lastyear['ygcg']) * 100;
        $calc['c57'] = $this->calc2($thisyear['ygcg'], $lastyear['ygcg']);

        $calc['v58'] = $thisyear['a17'] - $lastyear['a17'];
        $calc['v59'] = $thisyear['a17'] == 0 ? 0 : (($thisyear['a17'] - $lastyear['a17']) / $thisyear['a17']) * 100;

        $calc['v60'] = $thisyear['jgyxjf'] - $lastyear['jgyxjf'];
        $calc['f1'] = $thisyear['gwycgz'] - $lastyear['gwycgz'];
        $calc['f2'] = $thisyear['gwycwh'] - $lastyear['gwycwh'];
        $calc['f3'] = $thisyear['gwjdf'] - $lastyear['gwjdf'];
        $calc['cf1'] = $this->calc2($thisyear['gwycgz'], $lastyear['gwycgz']);
        $calc['cf2'] = $this->calc2($thisyear['gwycwh'], $lastyear['gwycwh']);
        $calc['cf3'] = $this->calc2($thisyear['gwjdf'], $lastyear['gwjdf']);
        // $calc['v61'] = $thisyear['jgyxjf'] == 0 ? 0 : (($thisyear['jgyxjf'] - $lastyear['jgyxjf']) / $thisyear['jgyxjf']) * 100;
        $calc['v61'] = $lastyear['jgyxjf'] == 0 ? 100 : 100 * (($thisyear['jgyxjf'] - $lastyear['jgyxjf']) / $lastyear['jgyxjf']);

        $calc['v62'] = $lastyear['gwycgz'] == 0 ? 100 : (($thisyear['gwycgz'] - $lastyear['gwycgz']) / $lastyear['gwycgz']) * 100;
        $calc['v63'] = $lastyear['gwjdf'] == 0 ? 100 : (($thisyear['gwjdf'] - $lastyear['gwjdf']) / $lastyear['gwjdf']) * 100;
        $calc['v64'] = $lastyear['gwycwh'] == 0 ? 100 : (($thisyear['gwycwh'] - $lastyear['gwycwh']) / $lastyear['gwycwh']) * 100;

        $calc['v70'] = $thisyear['total'] == 0 ? 100 : ($thisyear['gov_capital'] / $thisyear['total']) * 100;
        $calc['v71'] = $thisyear['gov_capital'] - $lastyear['gov_capital'];
        // $calc['v72'] = $thisyear['gov_capital'] == 0 ? 0 : 100 * (($thisyear['gov_capital'] - $lastyear['gov_capital']) / $thisyear['gov_capital']);
        $calc['v72'] = $lastyear['gov_capital'] == 0 ? 100 : (($thisyear['gov_capital'] - $lastyear['gov_capital']) / $lastyear['gov_capital']) * 100;

        // 事业单位
        $calc['v81'] = $this->calc1($thisyear['utility_income'], $lastyear['utility_income']);
        $calc['v82'] = $this->calc2($thisyear['utility_income'], $lastyear['utility_income']);
        $calc['v83'] = $this->calc3($thisyear['utility_income'], $thisyear['total']);

        $other = [];
        $other2 = [];
        foreach ($thisyear as $k => $v) {
            if (!in_array($k, ['project_count', 'ryinclude', 'gyinclude', 'ybggfwzc_arr'])) {
                $thisyear[$k] = $v / 10000;
            }
            if (in_array($k, ['shbzjyzc', 'wsjkzc', 'jnhbzc', 'cxsqzc', 'nlszc', 'zrzyhyqxzc', 'zfbzzc', 'zhfzzc', 'ybggfwzc']) && $v != 0) {
                array_push($other, $k);
            }
            if (in_array($k, ['a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9', 'a10', 'a11', 'a12', 'a13', 'a14']) && $v != 0) {
                array_push($other2, $k);
            }
        }
        foreach ($lastyear as $k => $v) {
            $lastyear[$k] = number_format($v / 10000, 2, ".", "");
        }

        foreach ($calc as $k => $v) {
            if (in_array($k, [
                'v2',
                'v5',
                'v8',
                'v11',
                'v14',
                'v17',
                'v26',
                'v28',
                'v30',
                'v32',
                'v34',
                'v36',
                'v38',
                'v40',
                'v42',
                'v44',
                'v46',
                'v48',
                'v50',
                'v52',
                'v54',
                'v56',
                'v58',
                'v60',
                'f1',
                'f2',
                'f3',
                'v71',
            ])) {
                $calc[$k] = $v / 10000;
            }
        }

        $include4 = "";

        if ($thisyear['general_public_budget'] > 0) {
            $include4 .= "一般公共预算拨款" . $this->formatunit($thisyear['general_public_budget']) . "万元、";
        }
        if ($thisyear['government_budgets'] > 0) {
            $include4 .= "政府性基金预算拨款" . $this->formatunit($thisyear['government_budgets']) . "万元、";
        }
        if ($thisyear['gov_capital'] > 0) {
            $include4 .= "国有资本经营预算" . $this->formatunit($thisyear['gov_capital']) . "万元、";
        }
        $thisyear['include4'] = trim($include4, "、") . "。";
        return [
            "thisyear" => $thisyear,
            "lastyear" => $lastyear,
            "calc" => $calc,
            "year" => date("Y"),
            "month" => date("m"),
            "day" => date("d"),
            "others" => $other,
            "others2" => $other2,
            "others3" => [
                'general_public_budget' => [
                    'title' => '一般公共',
                    'total' => $thisyear['general_public_budget']
                ],
                'government_budgets' => [
                    'title' => '政府性基金',
                    'total' => $thisyear['government_budgets']
                ],
                'gov_capital' => [
                    'title' => '国有资本',
                    'total' => $thisyear['gov_capital']
                ],
                // 'fiscal_appropriation' => [
                //     'title' => '财政拨款',
                //     'total' => $thisyear['fiscal_appropriation']
                // ],
                'utility_income' => [
                    'title' => '事业收入',
                    'total' => $thisyear['utility_income']
                ],
                'institution_management_income' => [
                    'title' => '事业单位经营收入',
                    'total' => $thisyear['institution_management_income']
                ],
                'institution_other_income' => [
                    'title' => '单位其他资金收入',
                    'total' => $thisyear['institution_other_income']
                ],
            ]
        ];
    }

    public function judge($a)
    {
        if ($a >= 0) {
            return '增加';
        } else {
            return '减少';
        }
    }
    public function judge2($a)
    {
        if ($a >= 0) {
            return '增长';
        } else {
            return '下降';
        }
    }
    public function sabs($a)
    {
        return str_replace("-", "", $a);
    }

    public function part3($thisyears, $lastyears)
    {
        // 一般公共预算 今年
        $arr = [];
        // 政府性基金
        $zfxjj = [];
        // 人员经费
        $ryzc = 0;
        $ryarr = [];
        // 公用经费
        $gyzc = 0;
        $gyarr = [];
        // 支出预算 - 按类汇总
        $zcbudget = [];
        foreach ($thisyears as $row) {
            $key = $row['functional_category_code_class'] . $row['functional_category_code_fund'] . $row['functional_category_code_item'];
            $title = $row['functional_category_name_class'] . '（类）' . $row['functional_category_name_fund'] . '（款）' . $row['functional_category_name_item'] . '（项）';

            // 一般公共预算
            if ($row['general_public_budget'] > 0) {
                if (isset($arr[$key])) {
                    $arr[$key]['total'] += $row['general_public_budget'];
                    $arr[$key]['c1'] += $row['general_public_budget'];
                } else {
                    $arr[$key] = [
                        'title' => $title,
                        'total' => $row['general_public_budget'],
                        'c1' => $row['general_public_budget'],
                        'c2' => 0,
                        'vmodel' => 'reason' . $key,
                        'c5' => 100
                    ];
                }
                if ($row['project_category_name'] == '基本支出') {
                    // 人员支出
                    if (in_array($row['economics_category_code_class'], ['301', '303'])) {
                        $ryzc += $row['general_public_budget'];
                        if (!in_array($row['economics_category_name'], $ryarr)) {
                            array_push($ryarr, $row['economics_category_name']);
                        }
                    }
                    // 公用支出
                    if ($row['economics_category_code_class'] == '302') {
                        $gyzc += $row['general_public_budget'];
                        if (!in_array($row['economics_category_name'], $gyarr)) {
                            array_push($gyarr, $row['economics_category_name']);
                        }
                    }
                }
                // 收入预算 - 按类汇总
                if (isset($zcbudget[$row['functional_category_code_class']])) {
                    $zcbudget[$row['functional_category_code_class']]['total'] += $row['general_public_budget'];
                } else {
                    $zcbudget[$row['functional_category_code_class']] = [
                        'total' => $row['general_public_budget'],
                        'title' => $row['functional_category_name_class'],
                        'use1' => $row['functional_category_name_fund'],
                        'use2' => $row['functional_category_name_item']
                    ];
                }
            }

            // 政府性基金
            if ($row['government_budgets'] > 0) {
                if (isset($zfxjj[$key])) {
                    $zfxjj[$key]['total'] += $row['government_budgets'];
                    $zfxjj[$key]['c1'] += $row['government_budgets'];
                } else {
                    $zfxjj[$key] = [
                        'title' => $title,
                        'total' => $row['government_budgets'],
                        'c1' => $row['government_budgets'],
                        'c2' => 0,
                        'vmodel' => 'zfreason' . $key,
                        'c5' => 100
                    ];
                }
            }
        }
        // 一般公共预算 去年
        $arr2 = [];
        // 政府性基金
        $zfxjj2 = [];
        foreach ($lastyears as $row) {
            $key = $row['functional_category_code_class'] . $row['functional_category_code_fund'] . $row['functional_category_code_item'];
            $title = $row['functional_category_name_class'] . '（类）' . $row['functional_category_name_fund'] . '（款）' . $row['functional_category_name_item'] . '（项）';

            if ($row['general_public_budget'] > 0) {
                if (isset($arr[$key])) {
                    $arr2[$key]['total'] += $row['general_public_budget'];
                } else {
                    $arr2[$key] = [
                        'title' => $title,
                        'total' => $row['general_public_budget'],
                        'c1' => 0,
                        'c2' => 0,
                        // 'vmodel' => 'reason20'
                    ];
                }
            }
            // 政府性基金
            if ($row['government_budgets'] > 0) {
                if (isset($arr[$key])) {
                    $zfxjj2[$key]['total'] += $row['government_budgets'];
                } else {
                    $zfxjj2[$key] = [
                        'title' => $title,
                        'total' => $row['government_budgets'],
                        'c1' => 0,
                        'c2' => 0,
                        // 'vmodel' => 'reason20'
                    ];
                }
            }
        }

        foreach ($arr as $key => $v) {
            if (isset($arr2[$key])) {
                $arr[$key]['c1'] = $arr[$key]['total'] - $arr2[$key]['total'];
                if ($arr2[$key]['total'] != 0) {
                    $arr[$key]['c5'] = round(($arr[$key]['c1'] / $arr2[$key]['total']) / 100, 2);
                }
            }
            $arr[$key]['c1'] = round($arr[$key]['c1'] / 10000, 2);
            $arr[$key]['total'] = round($arr[$key]['total'] / 10000, 2);
            $arr[$key]['c3'] = $arr[$key]['c1'] < 0 ? '减少' : '增加';
            $arr[$key]['c4'] = $arr[$key]['c1'] < 0 ? '下降' : '增长';
        }

        // 政府性基金
        foreach ($zfxjj as $key => $v) {
            if (isset($zfxjj2[$key])) {
                $zfxjj[$key]['c1'] = $zfxjj[$key]['total'] - $zfxjj2[$key]['total'];
                if ($zfxjj2[$key]['total'] != 0) {
                    $zfxjj[$key]['c5'] = round(($zfxjj[$key]['c1'] / $zfxjj2[$key]['total']) / 100, 2);
                }
            }
            $zfxjj[$key]['c1'] = round($zfxjj[$key]['c1'] / 10000, 2);
            $zfxjj[$key]['total'] = round($zfxjj[$key]['total'] / 10000, 2);
            $zfxjj[$key]['c3'] = $zfxjj[$key]['c1'] < 0 ? '减少' : '增加';
            $zfxjj[$key]['c4'] = $zfxjj[$key]['c1'] < 0 ? '下降' : '增长';
        }

        // 支出预算
        foreach ($zcbudget as $key => $v) {
            $zcbudget[$key]['total'] = round($zcbudget[$key]['total'] / 10000, 2);
        }


        return [
            "lists" => $arr,
            "gov" => $zfxjj,
            "zcbudget" => $zcbudget,
            "zdata" => [
                "ryzc" => $ryzc / 10000,
                "gyzc" => $gyzc / 10000,
                "ryarr" => implode(",", $ryarr),
                "gyarr" => implode(",", $gyarr)
            ]

        ];
    }
    // 第三部分数据 2
    public function part32($code)
    {
        // $code = $this->request->get('code', '');
        $dis_code = substr($code, 0, 6);
        $ins_code = substr($code, 6, 6);
        $thisyears = Db::query("select * from admin_budget where type = 1 and institutions_code = :code1 and district_code = :code2", ['code1' => $ins_code, 'code2' => $dis_code]);
        $lastyears = Db::query("select * from admin_budget where type = 2 and institutions_code = :code1 and district_code = :code2", ['code1' => $ins_code, 'code2' => $dis_code]);

        // 一般公共预算 今年
        $arr = [];
        // 政府性基金
        $zfxjj = [];
        // 人员经费
        $ryzc = 0;
        $ryarr = [];
        $ryarrsort = [];
        // 公用经费
        $gyzc = 0;
        $gyarr = [];
        $gyarrsort = [];
        // 支出预算 - 按一般公共预算汇总
        $zcbudget = [];
        // 支出预算 - 按total汇总
        $zcbudget2 = [];
        // 总计
        $total = 0;
        $table3 = [];
        $table9 = [];
        $table7 = [];
        $zf_jbzc = 0;
        $zf_xmzc = 0;

        // table 7 总计
        $total7 = 0;
        $table71 = 0;
        $table72 = 0;
        $table73 = 0;
        $table74 = 0;
        $table75 = 0;
        $table76 = 0;
        $table77 = 0;
        $table78 = 0;
        $table79 = 0;
        $table710 = 0;
        // table 4
        $table4 = $this->initTable4();
        $general_public_budget = 0;
        $government_budgets = 0;
        $gov_capital = 0;
        // $institution_other_income = 0;
        $fiscal_appropriation = 0;
        // 项目列表
        $project_list = [];

        foreach ($thisyears as $row) {
            if ($row['project_code'] && $row['project_category_name'] == '项目支出') {
                if (!in_array($row['project_code'], ['00010001', '00010002'])) {
                    if (isset($project_list[$row['project_code']])) {
                        $project_list[$row['project_code']]['budget'] += $row['general_public_budget'];
                    } else {
                        $project_list[$row['project_code']] = [
                            'project_name' => $row['project_name'],
                            'budget' => $row['general_public_budget'],
                            // 'info' => $row['project_info'],
                            'vmodel1' => 'vm1'.$row['project_code'],
                            'vmodel2' => 'vm2'.$row['project_code'],
                            'vmodel3' => 'vm3'.$row['project_code'],
                            'vmodel4' => 'vm4'.$row['project_code'],
                            'vmodel5' => 'vm5'.$row['project_code'],
                            'vmodel6' => 'vm6'.$row['project_code'],
                            'vmodel7' => 'vm7'.$row['project_code'],
                            'vmodel8' => 'vm8'.$row['project_code'],
                            'vmodel9' => 'vm9'.$row['project_code'],
                            'vmodel0' => 'vm10'.$row['project_code'],
                            'vmodel11' => 'vm11'.$row['project_code'],
                            'vmodel12' => 'vm12'.$row['project_code'],
                            'vmodel13' => 'vm13'.$row['project_code'],
                        ];
                    }
                }
            }
            $general_public_budget += $row['general_public_budget'];
            $government_budgets += $row['government_budgets'];
            $gov_capital += $row['government_capital_management_budget'];
            // $institution_other_income += $row['institution_other_income'];
            $fiscal_appropriation += $row['fiscal_appropriation'];

            $total += $row['general_public_budget'];
            $key = $row['functional_category_code_class'] . $row['functional_category_code_fund'] . $row['functional_category_code_item'];
            $title = $row['functional_category_name_class'] . '（类）' . $row['functional_category_name_fund'] . '（款）' . $row['functional_category_name_item'] . '（项）';

            // 一般公共预算
            if ($row['general_public_budget'] > 0) {
                if (isset($arr[$key])) {
                    $arr[$key]['total'] += $row['general_public_budget'];
                    $arr[$key]['c1'] += $row['general_public_budget'];
                } else {
                    $arr[$key] = [
                        'title' => $title,
                        'total' => $row['general_public_budget'],
                        'c1' => $row['general_public_budget'],
                        'c2' => 0,
                        'vmodel' => 'reason' . $key,
                        'c5' => 100
                    ];
                }
                if ($row['project_category_name'] == '基本支出') {
                    // 人员支出
                    if (in_array($row['economics_category_code_class'], ['301', '303'])) {
                        $ryzc += $row['general_public_budget'];
                        if (!in_array($row['economics_category_name'], $ryarr)) {
                            array_push($ryarr, $row['economics_category_name']);
                            $ryarrsort[$row['economics_category_code']] = $row['economics_category_name'];
                        }
                    }
                    // 公用支出
                    if ($row['economics_category_code_class'] == '302') {
                        $gyzc += $row['general_public_budget'];
                        if (!in_array($row['economics_category_name'], $gyarr)) {
                            array_push($gyarr, $row['economics_category_name']);
                            $gyarrsort[$row['economics_category_code']] = $row['economics_category_name'];
                        }
                    }
                }
                if ($row['project_category_name'] == '项目支出') {
                    $total7 += $row['general_public_budget'];
                    $table71 += $row['economics_category_name_class'] == "工资福利支出" ? $row['general_public_budget'] : 0;
                    $table72 += $row['economics_category_name_class'] == "商品和服务支出" ? $row['general_public_budget'] : 0;
                    $table73 += $row['economics_category_name_class'] == "对个人和家庭的补助" ? $row['general_public_budget'] : 0;
                    $table74 += $row['economics_category_name_class'] == "债务利息及费用支出" ? $row['general_public_budget'] : 0;
                    $table75 += $row['economics_category_name_class'] == "资本性支出（基本建设）" ? $row['general_public_budget'] : 0;
                    $table76 += $row['economics_category_name_class'] == "资本性支出" ? $row['general_public_budget'] : 0;
                    $table77 += $row['economics_category_name_class'] == "对企业补助（基本建设）" ? $row['general_public_budget'] : 0;
                    $table78 += $row['economics_category_name_class'] == "对企业补助" ? $row['general_public_budget'] : 0;
                    $table79 += $row['economics_category_name_class'] == "对社会保障基金补助" ? $row['general_public_budget'] : 0;
                    $table710 += $row['economics_category_name_class'] == "其他支出" ? $row['general_public_budget'] : 0;

                    // table 7
                    // level 1
                    $key7_1 = $row['functional_category_code_class'];
                    $key7_2 = $key7_1 . $row['functional_category_code_fund'];
                    $key7_3 = $key7_2 . $row['functional_category_code_item'];
                    // table 7 - level 1
                    if (isset($table7[$key7_1 . '0000'])) {
                        $table7[$key7_1 . '0000']['total'] += $row['general_public_budget'];
                        $table7[$key7_1 . '0000']['fuli'] += $row['economics_category_name_class'] == "工资福利支出" ? $row['general_public_budget'] : 0;
                        $table7[$key7_1 . '0000']['shangpin'] += $row['economics_category_name_class'] == "商品和服务支出" ? $row['general_public_budget'] : 0;
                        $table7[$key7_1 . '0000']['geren'] += $row['economics_category_name_class'] == "对个人和家庭的补助" ? $row['general_public_budget'] : 0;
                        $table7[$key7_1 . '0000']['zhaiwu'] += $row['economics_category_name_class'] == "债务利息及费用支出" ? $row['general_public_budget'] : 0;
                        $table7[$key7_1 . '0000']['zibenjianshe'] += $row['economics_category_name_class'] == "资本性支出（基本建设）" ? $row['general_public_budget'] : 0;
                        $table7[$key7_1 . '0000']['ziben'] += $row['economics_category_name_class'] == "资本性支出" ? $row['general_public_budget'] : 0;
                        $table7[$key7_1 . '0000']['qiyejianshe'] += $row['economics_category_name_class'] == "对企业补助（基本建设）" ? $row['general_public_budget'] : 0;
                        $table7[$key7_1 . '0000']['qiye'] += $row['economics_category_name_class'] == "对企业补助" ? $row['general_public_budget'] : 0;
                        $table7[$key7_1 . '0000']['baozhang'] += $row['economics_category_name_class'] == "对社会保障基金补助" ? $row['general_public_budget'] : 0;
                        $table7[$key7_1 . '0000']['qita'] += $row['economics_category_name_class'] == "其他支出" ? $row['general_public_budget'] : 0;
                        // $table7[$key7_1 . '0000']['jbzc'] += $row['project_category_name'] == "基本支出" ? $row['total'] : 0;
                        // $table7[$key7_1 . '0000']['xmzc'] += $row['project_category_name'] == "项目支出" ? $row['total'] : 0;
                    } else {
                        $table7[$key7_1 . '0000'] = [
                            'class' => $row['functional_category_code_class'],
                            'fund' => '', //$row['functional_category_code_fund'],
                            'item' => '', //$row['functional_category_code_item'],
                            'total' => $row['general_public_budget'],
                            // 'jbzc' => $row['project_category_name'] == "基本支出" ? $row['general_public_budget'] : 0,
                            // 'xmzc' => $row['project_category_name'] == "项目支出" ? $row['general_public_budget'] : 0,
                            'name' => $row['functional_category_name_class'],
                            'projectname' => '', //$row['project_name'],
                            'fuli' => $row['economics_category_name_class'] == "工资福利支出" ? $row['general_public_budget'] : 0,
                            'shangpin' => $row['economics_category_name_class'] == "商品和服务支出" ? $row['general_public_budget'] : 0,
                            'geren' => $row['economics_category_name_class'] == "对个人和家庭的补助" ? $row['general_public_budget'] : 0,
                            'zhaiwu' => $row['economics_category_name_class'] == "债务利息及费用支出" ? $row['general_public_budget'] : 0,
                            'zibenjianshe' => $row['economics_category_name_class'] == "资本性支出（基本建设）" ? $row['general_public_budget'] : 0,
                            'ziben' => $row['economics_category_name_class'] == "资本性支出" ? $row['general_public_budget'] : 0,
                            'qiyejianshe' => $row['economics_category_name_class'] == "对企业补助（基本建设）" ? $row['general_public_budget'] : 0,
                            'qiye' => $row['economics_category_name_class'] == "对企业补助" ? $row['general_public_budget'] : 0,
                            'baozhang' => $row['economics_category_name_class'] == "对社会保障基金补助" ? $row['general_public_budget'] : 0,
                            'qita' => $row['economics_category_name_class'] == "其他支出" ? $row['general_public_budget'] : 0,
                        ];
                    }
                    // table 7 - level 2
                    if (isset($table7[$key7_2 . '00'])) {
                        $table7[$key7_2 . '00']['total'] += $row['general_public_budget'];
                        $table7[$key7_2 . '00']['fuli'] += $row['economics_category_name_class'] == "工资福利支出" ? $row['general_public_budget'] : 0;
                        $table7[$key7_2 . '00']['shangpin'] += $row['economics_category_name_class'] == "商品和服务支出" ? $row['general_public_budget'] : 0;
                        $table7[$key7_2 . '00']['geren'] += $row['economics_category_name_class'] == "对个人和家庭的补助" ? $row['general_public_budget'] : 0;
                        $table7[$key7_2 . '00']['zhaiwu'] += $row['economics_category_name_class'] == "债务利息及费用支出" ? $row['general_public_budget'] : 0;
                        $table7[$key7_2 . '00']['zibenjianshe'] += $row['economics_category_name_class'] == "资本性支出（基本建设）" ? $row['general_public_budget'] : 0;
                        $table7[$key7_2 . '00']['ziben'] += $row['economics_category_name_class'] == "资本性支出" ? $row['general_public_budget'] : 0;
                        $table7[$key7_2 . '00']['qiyejianshe'] += $row['economics_category_name_class'] == "对企业补助（基本建设）" ? $row['general_public_budget'] : 0;
                        $table7[$key7_2 . '00']['qiye'] += $row['economics_category_name_class'] == "对企业补助" ? $row['general_public_budget'] : 0;
                        $table7[$key7_2 . '00']['baozhang'] += $row['economics_category_name_class'] == "对社会保障基金补助" ? $row['general_public_budget'] : 0;
                        $table7[$key7_2 . '00']['qita'] += $row['economics_category_name_class'] == "其他支出" ? $row['general_public_budget'] : 0;
                        // $table7[$key7_1 . '0000']['jbzc'] += $row['project_category_name'] == "基本支出" ? $row['total'] : 0;
                        // $table7[$key7_1 . '0000']['xmzc'] += $row['project_category_name'] == "项目支出" ? $row['total'] : 0;
                    } else {
                        $table7[$key7_2 . '00'] = [
                            'class' => $row['functional_category_code_class'],
                            'fund' => $row['functional_category_code_fund'],
                            'item' => '', //$row['functional_category_code_item'],
                            'total' => $row['general_public_budget'],
                            // 'jbzc' => $row['project_category_name'] == "基本支出" ? $row['general_public_budget'] : 0,
                            // 'xmzc' => $row['project_category_name'] == "项目支出" ? $row['general_public_budget'] : 0,
                            'name' => $row['functional_category_name_fund'],
                            'projectname' => '', //$row['project_name'],
                            'fuli' => $row['economics_category_name_class'] == "工资福利支出" ? $row['general_public_budget'] : 0,
                            'shangpin' => $row['economics_category_name_class'] == "商品和服务支出" ? $row['general_public_budget'] : 0,
                            'geren' => $row['economics_category_name_class'] == "对个人和家庭的补助" ? $row['general_public_budget'] : 0,
                            'zhaiwu' => $row['economics_category_name_class'] == "债务利息及费用支出" ? $row['general_public_budget'] : 0,
                            'zibenjianshe' => $row['economics_category_name_class'] == "资本性支出（基本建设）" ? $row['general_public_budget'] : 0,
                            'ziben' => $row['economics_category_name_class'] == "资本性支出" ? $row['general_public_budget'] : 0,
                            'qiyejianshe' => $row['economics_category_name_class'] == "对企业补助（基本建设）" ? $row['general_public_budget'] : 0,
                            'qiye' => $row['economics_category_name_class'] == "对企业补助" ? $row['general_public_budget'] : 0,
                            'baozhang' => $row['economics_category_name_class'] == "对社会保障基金补助" ? $row['general_public_budget'] : 0,
                            'qita' => $row['economics_category_name_class'] == "其他支出" ? $row['general_public_budget'] : 0,
                        ];
                    }
                    // table 7 - level 3
                    if (isset($table7[$key7_3])) {
                        $table7[$key7_3]['total'] += $row['general_public_budget'];
                        $table7[$key7_3]['fuli'] += $row['economics_category_name_class'] == "工资福利支出" ? $row['general_public_budget'] : 0;
                        $table7[$key7_3]['shangpin'] += $row['economics_category_name_class'] == "商品和服务支出" ? $row['general_public_budget'] : 0;
                        $table7[$key7_3]['geren'] += $row['economics_category_name_class'] == "对个人和家庭的补助" ? $row['general_public_budget'] : 0;
                        $table7[$key7_3]['zhaiwu'] += $row['economics_category_name_class'] == "债务利息及费用支出" ? $row['general_public_budget'] : 0;
                        $table7[$key7_3]['zibenjianshe'] += $row['economics_category_name_class'] == "资本性支出（基本建设）" ? $row['general_public_budget'] : 0;
                        $table7[$key7_3]['ziben'] += $row['economics_category_name_class'] == "资本性支出" ? $row['general_public_budget'] : 0;
                        $table7[$key7_3]['qiyejianshe'] += $row['economics_category_name_class'] == "对企业补助（基本建设）" ? $row['general_public_budget'] : 0;
                        $table7[$key7_3]['qiye'] += $row['economics_category_name_class'] == "对企业补助" ? $row['general_public_budget'] : 0;
                        $table7[$key7_3]['baozhang'] += $row['economics_category_name_class'] == "对社会保障基金补助" ? $row['general_public_budget'] : 0;
                        $table7[$key7_3]['qita'] += $row['economics_category_name_class'] == "其他支出" ? $row['general_public_budget'] : 0;
                        // $table7[$key7_1 . '0000']['jbzc'] += $row['project_category_name'] == "基本支出" ? $row['total'] : 0;
                        // $table7[$key7_1 . '0000']['xmzc'] += $row['project_category_name'] == "项目支出" ? $row['total'] : 0;
                    } else {
                        $table7[$key7_3] = [
                            'class' => $row['functional_category_code_class'],
                            'fund' => $row['functional_category_code_fund'],
                            'item' => $row['functional_category_code_item'],
                            'total' => $row['general_public_budget'],
                            // 'jbzc' => $row['project_category_name'] == "基本支出" ? $row['general_public_budget'] : 0,
                            // 'xmzc' => $row['project_category_name'] == "项目支出" ? $row['general_public_budget'] : 0,
                            'name' => $row['functional_category_name_item'],
                            'projectname' => $row['project_name'],
                            'fuli' => $row['economics_category_name_class'] == "工资福利支出" ? $row['general_public_budget'] : 0,
                            'shangpin' => $row['economics_category_name_class'] == "商品和服务支出" ? $row['general_public_budget'] : 0,
                            'geren' => $row['economics_category_name_class'] == "对个人和家庭的补助" ? $row['general_public_budget'] : 0,
                            'zhaiwu' => $row['economics_category_name_class'] == "债务利息及费用支出" ? $row['general_public_budget'] : 0,
                            'zibenjianshe' => $row['economics_category_name_class'] == "资本性支出（基本建设）" ? $row['general_public_budget'] : 0,
                            'ziben' => $row['economics_category_name_class'] == "资本性支出" ? $row['general_public_budget'] : 0,
                            'qiyejianshe' => $row['economics_category_name_class'] == "对企业补助（基本建设）" ? $row['general_public_budget'] : 0,
                            'qiye' => $row['economics_category_name_class'] == "对企业补助" ? $row['general_public_budget'] : 0,
                            'baozhang' => $row['economics_category_name_class'] == "对社会保障基金补助" ? $row['general_public_budget'] : 0,
                            'qita' => $row['economics_category_name_class'] == "其他支出" ? $row['general_public_budget'] : 0,
                        ];
                    }
                }
                // 收入预算 - 按一般公共预算汇总
                if (isset($zcbudget[$row['functional_category_code_class']])) {
                    $zcbudget[$row['functional_category_code_class']]['total'] += $row['general_public_budget'];
                } else {
                    $zcbudget[$row['functional_category_code_class']] = [
                        'total' => $row['general_public_budget'],
                        'title' => $row['functional_category_name_class'],
                        'use1' => $row['functional_category_name_fund'],
                        'use2' => $row['functional_category_name_item'],
                        'vmodel' => 'by' . $key,
                    ];
                }
            }
            // table 3 - level 1
            $key_1 = $row['functional_category_code_class'];
            $key_2 = $key_1 . $row['functional_category_code_fund'];
            $key_3 = $key_2 . $row['functional_category_code_item'];
            // table 3 - level 1
            if (isset($table3[$key_1 . '0000'])) {
                $table3[$key_1 . '0000']['total'] += $row['total'];
                $table3[$key_1 . '0000']['jbzc'] += $row['project_category_name'] == "基本支出" ? $row['total'] : 0;
                $table3[$key_1 . '0000']['xmzc'] += $row['project_category_name'] == "项目支出" ? $row['total'] : 0;
            } else {
                $table3[$key_1 . '0000'] = [
                    'class' => $row['functional_category_code_class'],
                    'fund' => '', //$row['functional_category_code_fund'],
                    'item' => '', //$row['functional_category_code_item'],
                    'total' => $row['total'],
                    'jbzc' => $row['project_category_name'] == "基本支出" ? $row['total'] : 0,
                    'xmzc' => $row['project_category_name'] == "项目支出" ? $row['total'] : 0,
                    'name' => $row['functional_category_name_class'],
                ];
            }
            // table 3 - level 2
            if (isset($table3[$key_2 . '00'])) {
                $table3[$key_2 . '00']['total'] += $row['total'];
                $table3[$key_2 . '00']['jbzc'] += $row['project_category_name'] == "基本支出" ? $row['total'] : 0;
                $table3[$key_2 . '00']['xmzc'] += $row['project_category_name'] == "项目支出" ? $row['total'] : 0;
            } else {
                $table3[$key_2 . '00'] = [
                    'class' => $row['functional_category_code_class'],
                    'fund' => $row['functional_category_code_fund'],
                    'item' => '', //$row['functional_category_code_item'],
                    'total' => $row['total'],
                    'jbzc' => $row['project_category_name'] == "基本支出" ? $row['total'] : 0,
                    'xmzc' => $row['project_category_name'] == "项目支出" ? $row['total'] : 0,
                    'name' => $row['functional_category_name_fund'],
                ];
            }
            // table 3 - level 3
            if (isset($table3[$key_3])) {
                $table3[$key_3]['total'] += $row['total'];
                $table3[$key_3]['jbzc'] += $row['project_category_name'] == "基本支出" ? $row['total'] : 0;
                $table3[$key_3]['xmzc'] += $row['project_category_name'] == "项目支出" ? $row['total'] : 0;
            } else {
                $table3[$key_3] = [
                    'class' => $row['functional_category_code_class'],
                    'fund' => $row['functional_category_code_fund'],
                    'item' => $row['functional_category_code_item'],
                    'total' => $row['total'],
                    'jbzc' => $row['project_category_name'] == "基本支出" ? $row['total'] : 0,
                    'xmzc' => $row['project_category_name'] == "项目支出" ? $row['total'] : 0,
                    'name' => $row['functional_category_name_item'],
                ];
            }

            // 收入预算 - 按total汇总
            if (isset($zcbudget2[$row['functional_category_code_class']])) {
                $zcbudget2[$row['functional_category_code_class']]['total'] += $row['total'];
            } else {
                $zcbudget2[$row['functional_category_code_class']] = [
                    'total' => $row['total'],
                    'title' => $row['functional_category_name_class'],
                    'use1' => $row['functional_category_name_fund'],
                    'use2' => $row['functional_category_name_item'],
                    'vmodel' => 'by' . $key,
                ];
            }

            // 政府性基金
            if ($row['government_budgets'] > 0) {
                $zf_jbzc += $row['project_category_name'] == "基本支出" ? $row['government_budgets'] : 0;
                $zf_xmzc += $row['project_category_name'] == "项目支出" ? $row['government_budgets'] : 0;
                if (isset($zfxjj[$key])) {
                    $zfxjj[$key]['total'] += $row['government_budgets'];
                    $zfxjj[$key]['c1'] += $row['government_budgets'];
                } else {
                    $zfxjj[$key] = [
                        'title' => $title,
                        'total' => $row['government_budgets'],
                        'c1' => $row['government_budgets'],
                        'c2' => 0,
                        'vmodel' => 'zfreason' . $key,
                        'c5' => 100
                    ];
                }

                // table 9 
                $key9_1 = $row['functional_category_code_class'];
                $key9_2 = $key9_1 . $row['functional_category_code_fund'];
                $key9_3 = $key9_2 . $row['functional_category_code_item'];
                // table 9 - level 1
                if (isset($table9[$key9_1 . '0000'])) {
                    $table9[$key9_1 . '0000']['total'] += $row['government_budgets'];
                    $table9[$key9_1 . '0000']['jbzc'] += $row['project_category_name'] == "基本支出" ? $row['government_budgets'] : 0;
                    $table9[$key9_1 . '0000']['xmzc'] += $row['project_category_name'] == "项目支出" ? $row['government_budgets'] : 0;
                } else {
                    $table9[$key9_1 . '0000'] = [
                        'class' => $row['functional_category_code_class'],
                        'fund' => '', //$row['functional_category_code_fund'],
                        'item' => '', //$row['functional_category_code_item'],
                        'total' => $row['government_budgets'],
                        'jbzc' => $row['project_category_name'] == "基本支出" ? $row['government_budgets'] : 0,
                        'xmzc' => $row['project_category_name'] == "项目支出" ? $row['government_budgets'] : 0,
                        'name' => $row['functional_category_name_class'],
                    ];
                }
                // table 9 - level 2
                if (isset($table9[$key9_2 . '00'])) {
                    $table9[$key9_2 . '00']['total'] += $row['government_budgets'];
                    $table9[$key9_2 . '00']['jbzc'] += $row['project_category_name'] == "基本支出" ? $row['government_budgets'] : 0;
                    $table9[$key9_2 . '00']['xmzc'] += $row['project_category_name'] == "项目支出" ? $row['government_budgets'] : 0;
                } else {
                    $table9[$key9_2 . '00'] = [
                        'class' => $row['functional_category_code_class'],
                        'fund' => $row['functional_category_code_fund'],
                        'item' => '', //$row['functional_category_code_item'],
                        'total' => $row['government_budgets'],
                        'jbzc' => $row['project_category_name'] == "基本支出" ? $row['government_budgets'] : 0,
                        'xmzc' => $row['project_category_name'] == "项目支出" ? $row['government_budgets'] : 0,
                        'name' => $row['functional_category_name_fund'],
                    ];
                }
                // table 9 - level 3
                if (isset($table9[$key9_3])) {
                    $table9[$key9_3]['total'] += $row['government_budgets'];
                    $table9[$key9_3]['jbzc'] += $row['project_category_name'] == "基本支出" ? $row['government_budgets'] : 0;
                    $table9[$key9_3]['xmzc'] += $row['project_category_name'] == "项目支出" ? $row['government_budgets'] : 0;
                } else {
                    $table9[$key9_3] = [
                        'class' => $row['functional_category_code_class'],
                        'fund' => $row['functional_category_code_fund'],
                        'item' => $row['functional_category_code_item'],
                        'total' => $row['government_budgets'],
                        'jbzc' => $row['project_category_name'] == "基本支出" ? $row['government_budgets'] : 0,
                        'xmzc' => $row['project_category_name'] == "项目支出" ? $row['government_budgets'] : 0,
                        'name' => $row['functional_category_name_item'],
                    ];
                }
            }
            // table 4
            if (isset($table4[$row['functional_category_code_class']])) {
                $table4[$row['functional_category_code_class']]['yiban'] += $row['general_public_budget'];
                $table4[$row['functional_category_code_class']]['zhengfu'] += $row['government_budgets'];
                $table4[$row['functional_category_code_class']]['guoyou'] += $row['government_capital_management_budget'];
                $table4[$row['functional_category_code_class']]['total'] += $row['general_public_budget'] + $row['government_budgets'] + $row['government_capital_management_budget'];
                // $table4[$row['functional_category_code_class']]['total'] += $row['total'];
            }
        }
        // 一般公共预算 去年
        $arr2 = [];
        // 政府性基金
        $zfxjj2 = [];
        foreach ($lastyears as $row) {
            $key = $row['functional_category_code_class'] . $row['functional_category_code_fund'] . $row['functional_category_code_item'];
            $title = $row['functional_category_name_class'] . '（类）' . $row['functional_category_name_fund'] . '（款）' . $row['functional_category_name_item'] . '（项）';

            if ($row['general_public_budget'] > 0) {
                if (isset($arr2[$key])) {
                    $arr2[$key]['total'] += $row['general_public_budget'];
                    // $arr2[$key]['c1'] += $row['general_public_budget'];
                } else {
                    $arr2[$key] = [
                        'title' => $title,
                        'total' => $row['general_public_budget'],
                        'c1' => 0,
                        'c2' => 0,
                        // 'vmodel' => 'reason20'
                    ];
                }
            }
            // 政府性基金
            if ($row['government_budgets'] > 0) {
                if (isset($zfxjj2[$key])) {
                    $zfxjj2[$key]['total'] += $row['government_budgets'];
                } else {
                    $zfxjj2[$key] = [
                        'title' => $title,
                        'total' => $row['government_budgets'],
                        'c1' => 0,
                        'c2' => 0,
                        // 'vmodel' => 'reason20'
                    ];
                }
            }
        }

        foreach ($arr as $key => $v) {
            if (isset($arr2[$key])) {
                $arr[$key]['c1'] = $arr[$key]['total'] - $arr2[$key]['total'];
                if ($arr2[$key]['total'] != 0) {
                    $arr[$key]['c5'] = number_format(($arr[$key]['c1'] / $arr2[$key]['total']) * 100, 2, ".", "");
                }
            }
            $arr[$key]['c1'] = number_format($arr[$key]['c1'] / 10000, 2, ".", "");
            $arr[$key]['total'] = number_format($arr[$key]['total'] / 10000, 2, ".", "");
            $arr[$key]['c3'] = $arr[$key]['c1'] < 0 ? '减少' : '增加';
            $arr[$key]['c4'] = $arr[$key]['c1'] < 0 ? '下降' : '增长';
            $arr[$key]['ltotal'] = isset($arr2[$key]) ? number_format($arr2[$key]['total'] / 10000, 2, ".", "") : 0;
        }

        // 政府性基金
        foreach ($zfxjj as $key => $v) {
            if (isset($zfxjj2[$key])) {
                $zfxjj[$key]['c1'] = $zfxjj[$key]['total'] - $zfxjj2[$key]['total'];
                if ($zfxjj2[$key]['total'] != 0) {
                    $zfxjj[$key]['c5'] = number_format(($zfxjj[$key]['c1'] / $zfxjj2[$key]['total']) * 100, 2, ".", "");
                }
            }
            $zfxjj[$key]['c1'] = number_format($zfxjj[$key]['c1'] / 10000, 2, ".", "");
            $zfxjj[$key]['total'] = number_format($zfxjj[$key]['total'] / 10000, 2, ".", "");
            $zfxjj[$key]['c3'] = $zfxjj[$key]['c1'] < 0 ? '减少' : '增加';
            $zfxjj[$key]['c4'] = $zfxjj[$key]['c1'] < 0 ? '下降' : '增长';
        }

        // 支出预算
        foreach ($zcbudget as $key => $v) {
            $zcbudget[$key]['rate'] = number_format(100 * ($zcbudget[$key]['total'] / $total), 2, ".", "");
            $zcbudget[$key]['total'] = number_format($zcbudget[$key]['total'] / 10000, 2, ".", "");
        }
        // 支出预算
        foreach ($zcbudget2 as $key => $v) {
            // $zcbudget[$key]['rate'] = number_format(100 * ($zcbudget[$key]['total'] / $total), 2, ".", "");
            $zcbudget2[$key]['total'] = $this->transformUnit($zcbudget2[$key]['total']);
        }
        // table 3
        foreach ($table3 as $k => $v) {
            $table3[$k]['total'] = $this->transformUnit($v['total']);
            $table3[$k]['jbzc'] = $this->transformUnit($v['jbzc']);
            $table3[$k]['xmzc'] = $this->transformUnit($v['xmzc']);
        }
        // table 9
        foreach ($table9 as $k => $v) {
            $table9[$k]['total'] = $this->transformUnit($v['total']);
            $table9[$k]['jbzc'] = $this->transformUnit($v['jbzc']);
            $table9[$k]['xmzc'] = $this->transformUnit($v['xmzc']);
        }
        // table 7
        foreach ($table7 as $k => $v) {
            $table7[$k]['total'] = $this->transformUnit($v['total']);
            $table7[$k]['fuli'] = $this->transformUnit($v['fuli']);
            $table7[$k]['shangpin'] = $this->transformUnit($v['shangpin']);
            $table7[$k]['geren'] = $this->transformUnit($v['geren']);
            $table7[$k]['zhaiwu'] = $this->transformUnit($v['zhaiwu']);
            $table7[$k]['zibenjianshe'] = $this->transformUnit($v['zibenjianshe']);
            $table7[$k]['ziben'] = $this->transformUnit($v['ziben']);
            $table7[$k]['qiyejianshe'] = $this->transformUnit($v['qiyejianshe']);
            $table7[$k]['qiye'] = $this->transformUnit($v['qiye']);
            $table7[$k]['baozhang'] = $this->transformUnit($v['baozhang']);
            $table7[$k]['qita'] = $this->transformUnit($v['qita']);
        }
        // table 4
        $i = 0;
        foreach ($table4 as $k => $v) {
            $table4[$k]['total'] = $this->transformUnit($v['total']);
            $table4[$k]['yiban'] = $this->transformUnit($v['yiban']);
            $table4[$k]['guoyou'] = $this->transformUnit($v['guoyou']);
            $table4[$k]['zhengfu'] = $this->transformUnit($v['zhengfu']);
            if ($i == 0) {
                $table4[$k]['xm'] = '财政拨款（补助）';
                $table4[$k]['hj'] = $this->transformUnit($fiscal_appropriation);
            }
            if ($i == 1) {
                $table4[$k]['xm'] = '一般公共预算';
                $table4[$k]['hj'] = $this->transformUnit($general_public_budget);
            }
            if ($i == 2) {
                $table4[$k]['xm'] = '政府性基金预算';
                $table4[$k]['hj'] = $this->transformUnit($government_budgets);
            }
            if ($i == 3) {
                $table4[$k]['xm'] = '国有资本经营预算';
                $table4[$k]['hj'] = $this->transformUnit($gov_capital);
            }
            $i++;
        }
        ksort($ryarrsort, 1);
        $_s1 = '';
        foreach ($ryarrsort as $k => $v) {
            $_s1 .= $v . "，";
        }
        ksort($gyarrsort, 1);
        $_s2 = '';
        foreach ($gyarrsort as $k => $v) {
            $_s2 .= $v . "，";
        }
        ksort($zcbudget, 1);
        ksort($table3, 1);
        ksort($table9, 1);
        ksort($table7, 1);
        foreach($project_list as $k=>$v){
            $project_list[$k]['budget'] = $this->transformUnit($v['budget'])."万元";
        }
        return [
            "year"=>date("Y"),
            "month"=>date("m"),
            "day"=>date("d"),
            "lists" => $arr,
            "projectlists" => $project_list,
            "gov" => $zfxjj,
            "zcbudget" => $zcbudget,
            "zcbudget2" => $zcbudget2,
            "zdata" => [
                "ryzc" => $ryzc / 10000,
                "gyzc" => $gyzc / 10000,
                // "ryarr" => implode(",", $ryarr),
                "ryarr" => trim($_s1, "，"),
                "gyarr" => trim($_s2, "，"),
                // "gyarr" => implode(",", $gyarr)
                'zf_jbzc' => $this->transformUnit($zf_jbzc),
                'zf_xmzc' => $this->transformUnit($zf_xmzc),
                'total7' => $this->transformUnit($total7),
                'table71' => $this->transformUnit($table71),
                'table72' => $this->transformUnit($table72),
                'table73' => $this->transformUnit($table73),
                'table74' => $this->transformUnit($table74),
                'table75' => $this->transformUnit($table75),
                'table76' => $this->transformUnit($table76),
                'table77' => $this->transformUnit($table77),
                'table78' => $this->transformUnit($table78),
                'table79' => $this->transformUnit($table79),
                'table710' => $this->transformUnit($table710),
            ],
            'table3' => $table3,
            'table9' => $table9,
            'table7' => $table7,
            'table4' => $table4,
            // 'arr2' => $arr2,
            // 'arr1' => $arr
            // 'rrr' => $ryarrsort

        ];
    }

    // 计算增长金额
    public function calc1($a, $b)
    {
        return $this->transformUnit($a - $b);
    }

    // 计算百分比
    public function calc2($a, $b)
    {
        if ($a == 0 && $b == 0) {
            return 0;
        }
        if ($b == 0) {
            return 100;
        }
        return 100 * (($a - $b) / $b);
    }
    // 计算total占比
    public function calc3($a, $b)
    {
        if ($b == 0) {
            return 100;
        }
        return 100 * ($a / $b);
    }
    public function transformUnit($a)
    {
        // return $a;
        return number_format($a / 10000, 2, ".", "");
    }
    public function formatunit($a)
    {
        return number_format($a, 2, ".", "");
    }
    public function keep2($a)
    {
        if (is_string($a)) {
            return $a;
        }
        return number_format($a, 2, ".", "");
    }
    public function initTable4()
    {
        $arr = [];
        $lists = Db::query("select DISTINCT(functional_category_code_class) as functional_category_code_class,functional_category_name_class from admin_budget");
        foreach ($lists as $row) {
            $arr[$row['functional_category_code_class']] = [
                'name' => $row['functional_category_name_class'],
                'code' => $row['functional_category_code_class'],
                'total' => 0,
                'yiban' => 0,
                'zhengfu' => 0,
                'guoyou' => 0,
                'xm' => '',
                'hj' => 0,
            ];
        }
        ksort($arr, 1);
        // $arr[0]['xm'] = 'xxxx';
        return $arr;
    }
    public function nptmp($arr)
    {
        $s = '';

        foreach ($arr as $v) {
            if ($v['total'] > 0) {
                $s .= $v['title'] . $this->keep2($v['total']) . "万元、";
            }
        }
        return trim($s, "、") . "。";
    }

    public function tmp3($arr, $reason)
    {
        $json = [];
        if ($reason != null) {
            $json = json_decode($reason, true);
        }
        $s = '';
        foreach ($arr as $v) {
            if ($v['total'] > 0) {
                $s .= $v['title'] . $this->keep2($v['total']) . "万元，主要用于：" . (isset($json[$v['vmodel']]) ? $json[$v['vmodel']] : '原因未说明') . ";";
            }
        }
        return trim($s, "；") . "。";
    }

    public function tmp4($arr)
    {
        $s = '<w:br/>';
        foreach ($arr as $v) {
            $s .= $v['title'] . $v['total'] . "万元，占" . $v['rate'] . "%。<w:br/>";
            // $s.= $v['title'].$v['total']."万元，占".$v['rate']."%。";
        }
        return $s;
    }

    public function tmp5($arr, $json)
    {
        $s = '<w:br/>';
        $i = 1;
        foreach ($arr as $v) {
            $s .= $i."、".$v['title'] . ":" . date("Y") . "年预算数为" . $v['total'] . "万元，比上年预算" . $this->judge($v['c1']).$this->sabs($v['c1'])."，".$this->judge2($v['c1']).$this->sabs($v['c5'])."%。主要原因是：".(isset($json[$v['vmodel']])?$json[$v['vmodel']]:"")."<w:br/><w:br/>";
            $i++;
        }
        return $s;
    }

    // public function enter1()
    // {
    //     $s = '</w:t></w:p>';
    //     return $s;
    // }

    // public function enter2() 
    // {
    //     $s = '<w:p>
    //     <w:pPr>
    //     <w:spacing w:line="360" w:lineRule="auto"/>
    //     <w:ind w:firstLine="480" w:firstLineChars="200"/>
    //     </w:pPr>
    //     <w:t>';
    //     return $s;
    // }

}
